duanla1996 2015-06-02 04:03
浏览 73
已采纳

逻辑布尔否定运算符优先级和关联

here's my first question(s) on StackOverflow, and as such I imagine it has been asked here before, but everything I type into the search bar gives me different questions. (Or sometimes "no" results at all!)

I am learning on w3Schools, but I saw this seemingly simple code snippet which stirred up a small parcel of questions:

In essence the code says:

if(! test === FALSE)
  display("test successful!");
else
  display("test failed...")

"test" in this particular case returns a string on success or a (Boolean) FALSE upon failure.

Here are the questions that stirred within me.

  1. As for the exclamation point (aka "Logical NOT" aka "Negation Operator") at the beginning of a conditional/if statement, is that:

    A) Applied to the whole statement within the parentheses?, or

    B) Only associated to "test"?

Note: The negation operator's associativity may not change the outcome in this instance, but its associativity would matter in a case like: (! FALSE || TRUE), yeah?

  1. I understand how the Negation Operator works on Booleans, but how does the Negation Operator behave when faced with a (PHP) string?

    A) Does the negation operator's behavior change if the string happens to be something tricky like "true" or "0"?

  2. Is (! test === FALSE) the same as (test !== FALSE)? Why didn't they just use "!=="?

HERE is the actual code in question:

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}

Further PHP question: Is there some sort of advantage to use filter_var() over filter_input() in this circumstance? Why did w3Schools use filter_var() and not filter_input()?

  • 写回答

1条回答 默认 最新

  • douzhuan1432 2015-06-02 04:38
    关注
    1. As you can see in the doc operators in PHP have different priorities. It means:! applied to $test first.

    2. According to the doc, empty string '' or '0' will be converted to false, otherwise true.

    3. It is not the same. When $test equals to empty string '' or '0' this two conditions have different behavior.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记