Verificare l'esistenza di una variabile (o del suo valore)
Updated at: 14/03/2014
Visto che ogni tanto capita di avere a che fare con isset() e simili, questo è il comportamento di php (perlomeno dalla versione 5.3.5, ma auspicabilmente anche prima)
| Value of variable ($var) |
isset($var) |
empty($var) |
is_null($var) |
| “” (an empty string) |
TRUE |
TRUE |
FALSE |
| ” ” (space) |
TRUE |
FALSE |
FALSE |
| FALSE |
TRUE |
TRUE |
FALSE |
| TRUE |
TRUE |
FALSE |
FALSE |
| array() (an empty array) |
TRUE |
TRUE |
FALSE |
| NULL |
FALSE |
TRUE |
TRUE |
| “0″ (0 as a string) |
TRUE |
TRUE |
FALSE |
| 0 (0 as an integer) |
TRUE |
TRUE |
FALSE |
| 0.0 (0 as a float) |
TRUE |
TRUE |
FALSE |
| var $var; (a variable declared, but without a value) |
FALSE |
TRUE |
TRUE |
| NULL byte (“\ 0″) |
TRUE |
FALSE |
FALSE |