Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the == operator transitive in PHP?

Tags:

equality

php

In JavaScript, the == operator isn't necessarily transitive:

js> '0' == 0
true
js> 0 == ''
true
js> '0' == ''
false

Is the same true in PHP? Can you give an example?

like image 293
mpen Avatar asked Jan 20 '11 23:01

mpen


People also ask

What does == mean in PHP?

Equal Operator == This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same.

What is == and === in php?

== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: operand1 == operand2. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

What is the effect of === operator?

1) When we compare two variables of different type e.g. a boolean with a string or a number with String using == operator, it automatically converts one type into another and return value based upon content equality, while === operator is strict equality operator in Java, and only return true if both variable of same ...

What is the equal operator?

The inequality operator ( != ) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types.


1 Answers

No, the == operator is not transitive.

The exact same scenario gives the same result in PHP.

echo var_dump('0'==0);
echo var_dump(0=='');
echo var_dump('0'=='');

yields:

boolean true
boolean true
boolean false 
like image 174
mpen Avatar answered Sep 19 '22 16:09

mpen