PHP Equality Example
We did not run through this example in class (due to time constraints), however, I highly recommend trying it! Which of the conditions in the if
statements below are false
? It is very interesting to note this!
<!DOCTYPE html>
<html>
<head>
<title>Second PHP Example</title>
</head>
<body>
<?php
// Ask PHP to print all errors and warnings
// This is very helpful when developing
// Do NOT do this on production
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Weird and strange behavior using ==
$x1 = "weird"; // normal string
$x2 = "10"; // string representing a number
$x3 = "0"; // string representing 0
$x4 = "00"; // string double 0
$x5 = "10 days"; // string representing a number
$y = 0; // 0 integer
$y2 = 10;
$z = false; // false boolean
if ($x3 == $y) {
print "[string] $x3 == [int] $y -- WEIRD! <br/>\n";
}
if ($x5 == $y2) {
print "[string] $x5 == [int] $y2 -- WEIRD! <br/>\n";
}
if ($x4 == $y) {
print "[string] $x4 == [int] $y -- WEIRD! <br/>\n";
}
if ($x1 == $z) {
print "[string] $x1 == [boolean] $z -- WEIRD! <br/>\n";
}
if ($x2 == $z) {
print "[string] $x2 == [boolean] $z -- WEIRD! <br/>\n";
}
if ($x3 == $z) {
print "[string] $x3 == [boolean] $z -- WEIRD! <br/>\n";
}
if ($x3) {
print "[string] $x3 is truthy -- WEIRD! <br/>\n";
}
if ($y == $z) {
print "[int] $y == [boolean] $z -- WEIRD! <br/>\n";
}
if ($x4 == $z) {
print "[string] $x4 == [boolean] $z -- WEIRD! <br/>\n";
}
?>
</body>
</html>