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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题