See in itself documentation.
This is a null:
$valor = null;
This is a string empty, nothing at all:
$valor = "";
So compares null, only one string empty is null too, is a complete non-sense, but PHP is like this:
if ($valor == null) {}
This checks if it is a string empty:
if ($valor == "") {}
It is often considered a better way to buy, but in this case it makes no real difference:
if ($valor === null) {}
Can also do:
if (is_null($valor)) {}
Documentation.
Or:
if (empty($valor)) {}
Documentation.
You’ll find a difference in cases like this:
$a = array();
echo $a == null; //true
echo $a === null; //false
I put in the Github for future reference.
It’s so confusing, I would avoid using nulls in PHP.
|
Empty() / ==null |
is_null() / ===null |
isset() |
array_key_exists() |
Φ |
T |
T |
F |
F |
null |
T |
T |
F |
T |
"" |
T |
F |
T |
T |
[] |
T |
F |
T |
T |
0 |
T |
F |
T |
T |
false |
T |
F |
T |
T |
true |
F |
F |
T |
T |
1 |
F |
F |
T |
T |
\0 |
F |
F |
T |
T |
Table taken from response from the OS.
At least in "normal" languages:
Identical sign "===" is only used in PHP? Why? and Doubt about === e !=
– rray
Related: https://answall.com/questions/73007/qual-o-mais-r%C3%A1pido-is-nully-ou-y-null
– KaduAmaral