Created
March 25, 2020 09:56
-
-
Save Steven24K/6009d549b6b1c30932a043dbf3eea134 to your computer and use it in GitHub Desktop.
An experiment for comparing boolean expressions in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function toString($value) { | |
if ($value === TRUE) return 'TRUE'; | |
if ($value === FALSE) return 'FALSE'; | |
if ($value === null) return 'null'; | |
if (is_string($value)) return '"' . $value . '"'; | |
return $value; | |
} | |
function pick_color($v) { | |
if ($v) return 'green'; | |
return 'red'; | |
} | |
$values = [TRUE, FALSE, 'TRUE', 'FALSE', 1, 0, -1, '1', '0', '-1', 'foo', '', null]; | |
//php -S localhost:3000 | |
?> | |
<style> | |
#compare-table { | |
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; | |
border-collapse: collapse; | |
width: 100%; | |
} | |
#compare-table td, #compare-table th { | |
border: 1px solid #ddd; | |
padding: 8px; | |
} | |
#compare-table tr:nth-child(even){background-color: #f2f2f2;} | |
#compare-table tr:hover {background-color: #ddd;} | |
#compare-table th { | |
padding-top: 12px; | |
padding-bottom: 12px; | |
text-align: left; | |
background-color: #4CAF50; | |
color: white; | |
} | |
</style> | |
<h1>Using ==</h1> | |
<table id="compare-table"> | |
<thead> | |
<tr> | |
<th></th> | |
<?php | |
foreach ($values as $value) { | |
echo '<th>' . toString($value) . '</th>'; | |
} | |
?> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
for ($y = 0; $y < count($values); $y++) { | |
echo '<tr>'; | |
echo '<th>' . toString($values[$y]) . '</th>'; | |
for ($x = 0; $x < count($values); $x++) { | |
echo '<td style="background-color: '. pick_color($values[$y] == $values[$x]) .'">' . toString($values[$y] == $values[$x]) . '</td>'; | |
} | |
echo '</tr>'; | |
} | |
?> | |
</tbody> | |
</table> | |
<h1>Using ===</h1> | |
<table id="compare-table"> | |
<thead> | |
<tr> | |
<th></th> | |
<?php | |
foreach ($values as $value) { | |
echo '<th>' . toString($value) . '</th>'; | |
} | |
?> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
for ($y = 0; $y < count($values); $y++) { | |
echo '<tr>'; | |
echo '<th>' . toString($values[$y]) . '</th>'; | |
for ($x = 0; $x < count($values); $x++) { | |
echo '<td style="background-color: '. pick_color($values[$y] === $values[$x]) .'">' . toString($values[$y] === $values[$x]) . '</td>'; | |
} | |
echo '</tr>'; | |
} | |
?> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment