Memory App
 



Building memory bonds
Php  >     ZCE 5.3  |  Patterns  |  Modern Php Php   >   Operators   >   Comparison

Equivalence Operator

== / != Evaluates to true if the two operands are equivalent.

Identity Operator

=== / !== Evaluates to true only if the operands are of the same data type and have the same value.

$domain = 'xn--google.com';
echo $finded = (stripos($domain, 'xn--') === 0) ? "true" : "false";
    // Output: true

$domain = 'google.com';
echo $finded = (stripos($domain, 'xn--') == 0) ? "true" : "false";
    // Output: true
    // Wrong (FALSE == 0 returns TRUE)
    // Identical operator must be used insteed
It's easy to confuse the assignment operator = for the comparison operator ==
    
echo $a == 10;
echo 10 == $a; // better
These two operations are completely identical, but, because the left-hand operator of an assignment must be a variable, if you had forgotten one of the equal signs, the parser would have thrown an error, thus alerting you to your mistake.

$a = 9;
if ($a == 10) {}
if ($a = 10) {} // No alert error
if (10 = $a) {} //  Parse error: syntax error, unexpected '='

Inequality

< / <= / > / >= While the process is clear for numbers, things change a bit for other data types.

$left = "ABC";
$right = "ABD";
echo (int) ($left > $right); // output 0
    // because the letter D in $right is higher 
    // than the corresponding letter C in $left

$left = 'apple';
$right = 'Apple';
echo (int) ($left > $right); // output 1
    // because the ASCII value of the character a (97) is
    // than that of the character A (65)