Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 469
Newb Avatar asked Dec 04 '09 11:12

Newb


People also ask

What is difference between != and <> operator in PHP?

<> is exactly the same as != operator since both of them are parsed as T_IS_NOT_EQUAL token. And they have same precedence.

How do I check if two strings are not equal in PHP?

PHP strcmp() Function: The strcmp() is an inbuilt function in PHP that is used to compare two strings.

What is the use of === operator in PHP?

Identical Operator === The comparison operator called as the Identical operator is the triple equal sign “===”. This operator allows for a much stricter comparison between the given variables or values. This operator returns true if both variable contains same information and same data types otherwise return false.

Which is faster == or === PHP?

Equality operator == converts the data type temporarily to see if its value is equal to the other operand, whereas === (the identity operator) doesn't need to do any type casting and thus less work is done, which makes it faster than ==.


1 Answers

== 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)!

like image 85
Ben James Avatar answered Sep 19 '22 23:09

Ben James