开发者

php is not equal to and is not equal,equal to

I keep seeing variations of this:

Not equal !=

开发者_开发知识库

Not equal, equal

!==

Which one is the standard or do they have different meanings?

I am guessing the latter also checks the value and the name if it's a string, while the former might just check the value only...


== and != check equality by value, and in PHP you can compare different types in which certain values are said to be equivalent.

For example, "" == 0 evaluates to true, even though one is a string and the other an integer.

=== and !== check the type as well as the value.

So, "" === 0 will evaluate to false.


Edit: To add another example of how this "type-juggling" may catch you out, try this:

var_dump("123abc" == 123);

Gives bool(true)!


The second one is type-strict.

"1" != 1;  // false
"1" !== 1; // true because the first is a string, the second is a number


!= not equal by value

!== not equal by value and type


in an example:

"2" == 2 -> true

"2" === 2 -> false


"2" !== 2 -> true

"2" != 2 -> false

this is also important when you use certain function that can return 0 or false

for example strpos: you want always to check types too there, not only values. because 0 == false but 0 !== false.

since strpos can return 0 if a string is at the first position. but that not the same as false, which means the string has not been found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜