Variable types are also important in comparison.When you compare two variables
with the identical operator (===), like this, the active types for the zvals are compared,
and if they are different, the comparison fails outright:
$a = 0;
$b = ‘0’;
echo ($a === $b)?”Match”:”Doesn’t Match”;
For that reason, this example fails.
With the is equal operator (==), the comparison that is performed is based on the
active types of the operands. If the operands are strings or nulls, they are compared as
strings, if either is a Boolean, they are converted to Boolean values and compared, and
otherwise they are converted to numbers and compared.Although this results in the ==
operator being symmetrical (for example, if $a == $bis the same as $b == $a), it actually
is not transitive.The following example of this was kindly provided by Dan Cowgill:
$a = “0”;
$b = 0;
$c = “”;
echo ($a == $b)?”True”:”False”; // True
echo ($b == $c)?”True”:”False”; // True
echo ($a == $c)?”True”:”False”; // False
Although transitivity may seem like a basic feature of an operator algebra, understanding
how ==works makes it clear why transitivity does not hold. Here are some examples:
n “0” == 0because both variables end up being converted to integers and compared.
n $b == $cbecause both $band$care converted to integers and compared.
n However,$a != $cbecause both $aand$care strings, and when they are compared as strings,
they are decidedly different.
In his commentary on this example, Dan compared this to the ==andeqoperators in
Perl, which are both transitive.They are both transitive, though, because they are both
typed comparison.==in Perl coerces both operands into numbers before performing the
comparison, whereas eqcoerces both operands into strings.The PHP ==is not a typed
comparator, though, and it coerces variables only if they are not of the same active type.
Thus the lack of transitivity.
with the identical operator (===), like this, the active types for the zvals are compared,
and if they are different, the comparison fails outright:
$a = 0;
$b = ‘0’;
echo ($a === $b)?”Match”:”Doesn’t Match”;
For that reason, this example fails.
With the is equal operator (==), the comparison that is performed is based on the
active types of the operands. If the operands are strings or nulls, they are compared as
strings, if either is a Boolean, they are converted to Boolean values and compared, and
otherwise they are converted to numbers and compared.Although this results in the ==
operator being symmetrical (for example, if $a == $bis the same as $b == $a), it actually
is not transitive.The following example of this was kindly provided by Dan Cowgill:
$a = “0”;
$b = 0;
$c = “”;
echo ($a == $b)?”True”:”False”; // True
echo ($b == $c)?”True”:”False”; // True
echo ($a == $c)?”True”:”False”; // False
Although transitivity may seem like a basic feature of an operator algebra, understanding
how ==works makes it clear why transitivity does not hold. Here are some examples:
n “0” == 0because both variables end up being converted to integers and compared.
n $b == $cbecause both $band$care converted to integers and compared.
n However,$a != $cbecause both $aand$care strings, and when they are compared as strings,
they are decidedly different.
In his commentary on this example, Dan compared this to the ==andeqoperators in
Perl, which are both transitive.They are both transitive, though, because they are both
typed comparison.==in Perl coerces both operands into numbers before performing the
comparison, whereas eqcoerces both operands into strings.The PHP ==is not a typed
comparator, though, and it coerces variables only if they are not of the same active type.
Thus the lack of transitivity.