Does PHP negation check with `!` coprrespond to `!=` or to `!==`?

て烟熏妆下的殇ゞ 提交于 2020-01-30 13:09:27

问题


In PHP, is

if(!$foo)

equivalent with

if($foo != true)

or with

if($foo !== true)

or is it even something completly different of both?


回答1:


if(!$foo)

is the equivalent to

if($foo != true)

so

$foo = null;
if(!$foo){
 echo "asd";
}

will ouptut "asd"




回答2:


Note that,

== OR != compares the values of variables for equality, type casting as necessary. === OR !== checks if the two variables are of the same type AND have the same value.

This answer will give you better explanation of this concept: https://stackoverflow.com/a/80649/3067928




回答3:


Its not the same

!= is No equal (Returns true if  is not equal)
!== is Not identical  (Returns true if  is not equal , or they are not of the same type)



回答4:


$a != $b

TRUE if $a is not equal to $b after type juggling.

$a !== $b

TRUE if $a is not equal to $b, or they are not of the same type.


See type juggling in PHP for more info on type juggling.


Sources : php.net



来源:https://stackoverflow.com/questions/27319742/does-php-negation-check-with-coprrespond-to-or-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!