Skip to content

Instantly share code, notes, and snippets.

@alex-phillips
Last active August 29, 2015 14:09
Show Gist options
  • Save alex-phillips/6a54d0cc0705cae94b15 to your computer and use it in GitHub Desktop.
Save alex-phillips/6a54d0cc0705cae94b15 to your computer and use it in GitHub Desktop.
PHP vs JS Loose Comparisons with '=='
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Problem, Boole?</title>
<style>
body {
font-family: Verdana, Geneva, sans-serif;
}
td {
padding: 5px;
}
thead tr td {
text-align: center;
}
.th_color {
background-color: MediumPurple;
}
tbody tr td {
background-color: WhiteSmoke;
}
.td_diff {
background-color: LightCoral;
}
</style>
</head>
<body>
<?php
$a = array(
true,
false,
1,
0,
-1,
"1",
"0",
"-1",
null,
array(),
"php",
""
);
$al = array(
"TRUE",
"FALSE",
1,
0,
-1,
"\"1\"",
"\"0\"",
"\"-1\"",
"NULL",
"array()",
"\"php\"",
"\"\""
);
?>
<table>
<thead>
<tr>
<td colspan="13">PHP vs. JS Loose comparisons with ==</td>
</tr>
<tr class="th_color">
<td></td><?php foreach ($al as $v) {
echo "<td>" . $v . "</td>";
} ?></tr>
</thead>
<tbody>
<?php
foreach ($a as $i => $r) {
echo " <tr>";
foreach ($a as $j => $c) {
echo ($j ? "" : "<td>" . $al[$i] . "</td>") . "<td>" . ($r == $c
? "TRUE" : "FALSE") . "</td>";
}
echo "</tr>\n";
}
?>
</tbody>
</table>
<script
src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function () {
var a = [true, false, 1, 0, -1, "1", "0", "-1", null, [], "js", ""];
$.each(a, function (i, r) {
$.each(a, function (j, c) {
var d = $('tbody tr:nth-child(' + (i + 1) + ') td:nth-child(' + (j + 2) + ')');
if (d.text() !== (r == c ? "TRUE" : "FALSE")) d.addClass('td_diff');
});
});
});
</script>
<p style="width: 800px">
NOTE: The comparison texts(TRUE/FALSE) are all PHP == comparisons, the red
table cells are where the JavaScript == comparisons differ from the PHP ==
ones.
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment