Php is not equal and not equal to

I continue to see variations of this:

Not equal !=

Not equal, equal

!==

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

I assume the latter also checks the value and name if it is a string, while the former can just check the value only ...

+6
operators equality php
source share
4 answers

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

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

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

So, "" === 0 will be evaluated to false .


Edit: To add another example of how this "type manipulator" can catch you, try the following:

 var_dump("123abc" == 123); 

Gives bool(true) !

+19
source share

The second is strict.

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

!= not equal to

!== not equal to the value and enter

+5
source share

in the example:

 "2" == 2 -> true "2" === 2 -> false "2" !== 2 -> true "2" != 2 -> false 

it is also important when you use a specific function that can return 0 or false

e.g. strpos: you should always check types, not just values. because 0 == false , but 0 !== false .

since strpos can return 0 if the string is in the first position. but this is not the same as false, which means the string was not found.

+1
source share

All Articles