if statement - Type-juggling and (strict) greater/lesser-than comparisons in PHP -
php famous type-juggling. must admit puzzles me, , i'm having hard time find out basic logical/fundamental things in comparisons.
for example: if $a > $b true , $b > $c true, must mean $a > $c always true too?
following basic logic, yes i'm puzzled not trust php in this. maybe can provide example not case?
also i'm wondering strict lesser-than , strict greater-than operators (as meaning described strictly knew in past equality comparisons) if makes difference if left , right operands swapped strictly unequal values:
# precondition: if ($a === $b) { throw new exception( 'both strictly equal - can not compare strictly greater or smaller' ); } ($a > $b) !== ($b > $a) for of type comparison combinations these greater / lesser comparison operators not documented, reading manual not helpful in case.
php's comparison operators deviate computer-scientific definitions in several ways:
in order constitute equivalence relation == has reflexive, symmetric , transitive:
php's
==operator not reflexive, i.e.$a == $anot true:var_dump(nan == nan); // bool(false)note: fact comparison involving
nanfalsenot specific php. mandated ieee 754 standard floating-point arithmetic (more info).php's
==operator symmetric, i.e.$a == $b,$b == $asame.php's
==operator not transitive, i.e.$a == $b,$b == $cnot follows$a == $c:var_dump(true == "a"); // bool(true) var_dump("a" == 0); // bool(true) var_dump(true == 0); // bool(false)
in order constitute partial order <=/>= has reflexive, anti-symmetric , transitive:
php's
<=operator not reflexive, i.e.$a <= $anot true (example same==).php's
<=operator not anti-symmetric, i.e.$a <= $b,$b <= $anot follow$a == $b:var_dump(nan <= "foo"); // bool(true) var_dump("foo" <= nan); // bool(true) var_dump(nan == "foo"); // bool(false)php's
<=operator not transitive, i.e.$a <= $b,$b <= $cnot follow$a <= $c(example same==).extra: php's
<=operator not total, i.e. both$a <= $b,$b <= $acan false:var_dump(new stdclass <= new datetime); // bool(false) var_dump(new datetime <= new stdclass); // bool(false)
in order constitute strict partial order </> has irreflexive, asymmetric , transitive:
php's
<operator irreflexive, i.e.$a < $anever true. note true only of php 5.4.inf < infevaluatedtrue.php's
<operator not asymmetric, i.e.$a < $bnot follow!($b < $a)(example same<=not being anti-symmetric).php's
<operator not transitive, i.e.$a < $b,$b < $cnot follow$a < $c:var_dump(-inf < 0); // bool(true) var_dump(0 < true); // bool(true) var_dump(-inf < true); // bool(false)extra: php's
<operator not trichotomous, i.e. of$a < $b,$b < $a,$a == $bcan false (example same<=not being total).extra: php's
<operator can circular, i.e. possible$a < $b,$b < $c,$c < $a:var_dump(inf < []); // bool(true) var_dump([] < new stdclass); // bool(true) var_dump(new stdclass < inf); // bool(true)note: above example throws "object of class stdclass not converted double" notice.
you can find few nice graphs php's comparison operators on php sadness 52 - comparison operators.
as last note, want point out there 2 equalities php does guarantee (unlike pretty else). these 2 hold, because compiler reduces 1 other:
($a > $b) == ($b < $a) ($a >= $b) == ($b <= $a)
Comments
Post a Comment