minte9
LearnRemember / PHP



Identity

True if only operands are of the same data type and the same value.
 
/**
 * Identical operator
 */

$x1 = 'xn--google.com';
$x2 = 'google.com';

var_dump(stripos($x1, 'xn--') === 0); // true - Correct
var_dump(stripos($x2, 'xn--') == 0);  // true - Incorrect

Confusion

It's easy to confuse the assignment operator = for the comparison operator ==
 
/**
 * Comparition operator, prone to error
 */

$a = 10;
$b = 9;

var_dump($a == 10);     // true
var_dump(10 == $a);     // true, better

if ($b = 10) echo $b;   // 10 - Incorrect

Inequality

While the process is clear for numbers, things change a bit for other data types.
 
/** 
 * Comparing strings
 *
 * D > C, a > A 
 * 
 * ASCII value of a (97)
 * ASCII value of A (65)
 */

var_dump("ABC" > "ABD");        // false
var_dump("apple" > "Apple");    // true






Questions and answers




stripos('ab', 'a') === 0

  • a) TRUE
  • b) FALSE, strings are not equal

stripos('bc', 'a') == 0

  • a) TRUE
  • b) FALSE, strings are not equal

Which one return a parse error?

  • a) if (10 = $a) {}
  • b) if (10 == $a) {}

Which returns true?

  • a) 'apple' > 'Apple'
  • b) 'Apple' > 'apple'

What's the ASCII values of "a" and "A"?

  • a) 97 and 65
  • b) 65 and 97


References