Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created February 5, 2014 18:26
Show Gist options
  • Save loganlinn/8830023 to your computer and use it in GitHub Desktop.
Save loganlinn/8830023 to your computer and use it in GitHub Desktop.
<?php
$a = array();
$vals = [
['"" ', ""],
['0 (int) ', 0],
['0.0 (float) ', 0.0],
['"0" (string) ', "0"],
['FALSE ', false],
['NULL ', null],
['array() ', []],
];
echo "\nisset()\n";
foreach ($vals as list($desc, $value)) {
echo "\t", $desc, " ", isset($value) ? "TRUE" : "FALSE", "\n";
}
echo "\t", '$var (declared, no value) ', " ", isset($var) ? "TRUE" : "FALSE", "\n";
echo "\t", '$a["x"] (undefined offset)', " ", isset($a['x']) ? "TRUE" : "FALSE", "\n";
echo "\nempty()\n";
foreach ($vals as list($desc, $value)) {
echo "\t", $desc, " ", empty($value) ? "TRUE" : "FALSE", "\n";
}
echo "\t", '$var (declared, no value) ', " ", empty($var) ? "TRUE" : "FALSE", "\n";
echo "\t", '$a["x"] (undefined offset)', " ", empty($a['x']) ? "TRUE" : "FALSE", "\n";
echo "\nfalseyness\n";
foreach ($vals as list($desc, $value)) {
echo "\t", $desc, " ", $value ? "TRUE" : "FALSE", "\n";
}
echo "\t", '$var (declared, no value) ', " ", @($var ? "TRUE" : "FALSE"), " (PHP Notice)\n";
echo "\t", '$a["x"] (undefined offset)', " ", @($a['x'] ? "TRUE" : "FALSE"), " (PHP Notice)\n";
/*
* isset()
* "" TRUE
* 0 (int) TRUE
* 0.0 (float) TRUE
* "0" (string) TRUE
* FALSE TRUE
* NULL FALSE
* array() TRUE
* $var (declared, no value) FALSE
* $a["x"] (undefined offset) FALSE
*
* empty()
* "" TRUE
* 0 (int) TRUE
* 0.0 (float) TRUE
* "0" (string) TRUE
* FALSE TRUE
* NULL TRUE
* array() TRUE
* $var (declared, no value) TRUE
* $a["x"] (undefined offset) TRUE
*
* falseyness
* "" FALSE
* 0 (int) FALSE
* 0.0 (float) FALSE
* "0" (string) FALSE
* FALSE FALSE
* NULL FALSE
* array() FALSE
* $var (declared, no value) FALSE (PHP Notice)
* $a["x"] (undefined offset) FALSE (PHP Notice)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment