PROGRAMMING     CLIMBING

  minte9
reminder




Last update:   18-05-2022

Identity

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

$domain = 'xn--google.com';
echo (stripos($domain, 'xn--') === 0); // true - Correct

// Equal

$domain2 = 'google.com';
echo (stripos($domain2, 'xn--') == 0); // true - Incorrect
It's easy to confuse the assignment operator = for the comparison operator ==
 
$a = 10;

echo $a == 10; // prone to error
echo 10 == $a; // better

$a = 9;

if ($a == 10) {}
if ($a = 10) {} // No alert error
if (10 == $a) {} // Better

Inequality

While the process is clear for numbers, things change a bit for other data types.
 
/** 
 * Comparing strings
 */

$left = "ABC";
$right = "ABD";

echo (int) ($left > $right); // 0

    // because the letter D in $right is higher ...
    // than the corresponding letter C in $left

$left = 'apple';
$right = 'Apple';

echo (int) ($left > $right); // 1

    // because the ASCII value of the character a (97) is ...
    // than that of the character A (65)

Questions     github Operators
Logical
        A B C D E F
🔔
1/5