Home >Backend Development >PHP Tutorial >PHP equality comparison and empty, isset, isnull
Variable is empty
The following are considered empty:
- "" (empty string)
- 0 (0 as an integer)
- 0.0 (0 as a floating point number)
- "0 ” (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- $var; (a variable declared but without a value)
The null value in the judgment statement returns false , the following code will not produce any output, because the judgments in the if statement are all false:
<code><span>$emptyList</span> = [<span>""</span>, <span>0</span>, <span>0.0</span>, <span>"0"</span>, <span>NULL</span>, <span>FALSE</span>, <span>array</span>(), <span>$var</span>, ]; <span>foreach</span>(<span>$emptyList</span><span>as</span><span>$val</span>) { <span>if</span> (<span>$val</span>) { var_dump(<span>$val</span>); } }</code>
Comparison operators: ==
and ===
==
only involve the comparison of values <code>var_dump(<span>0</span> == <span>'0.0'</span>); <span>//</span><span>true</span> var_dump(<span>0</span> == <span>''</span>); <span>//</span><span>true</span> var_dump(<span>0</span> == <span>false</span>); <span>//</span><span>true</span></code>
===
It also involves the comparison of values and types, which is more strict. <code>var_dump(<span>0</span> === <span>'0.0'</span>); <span>//</span><span>false</span> var_dump(<span>0</span> === <span>''</span>); <span>//</span><span>false</span> var_dump(<span>0</span> === <span>false</span>); <span>//</span><span>false</span></code>
==
. Except for "0", all return true:<code><span><span><?php</span><span>$emptyList</span> = [<span>""</span>, <span>0</span>, <span>0.0</span>, <span>"0"</span>, <span>NULL</span>, <span>FALSE</span>, <span>array</span>(), <span>$var</span>, ]; <span>foreach</span>(<span>$emptyList</span><span>as</span><span>$val</span>) { var_dump(<span>$val</span> == <span>null</span>); }</span></code>
empty , isset, isnull
<code><?php <span>$emptyList</span> = [<span>""</span>, <span>0</span>, <span>0.0</span>, <span>"0"</span>, NULL, FALSE, array(), <span>$var</span>, ]; foreach (<span>$emptyList</span> as <span>$e</span>) { var_dump(empty(<span>$e</span>)); } /* bool(<span>true</span>) bool(<span>true</span>) bool(<span>true</span>) bool(<span>true</span>) bool(<span>true</span>) bool(<span>true</span>) bool(<span>true</span>) bool(<span>true</span>) */</code>
<code><span>$var</span> = <span>1</span>; var_dump(<span>isset</span>(<span>$var</span>)); <span>// true</span><span>unset</span>(<span>$var</span>); var_dump(<span>isset</span>(<span>$var</span>)); <span>// false</span><span>$var</span> = <span>null</span>; var_dump(<span>isset</span>(<span>$var</span>)); <span>// false</span></code>
=== null
. Note that the
== comparison between null values may not necessarily return true:
<code><span>// 不具传递性</span><span>var_dump(<span>0</span> == <span>'0'</span>)</span>; <span>// true</span><span>var_dump(<span>''</span> == <span>0</span> )</span>; <span>// true</span><span>var_dump(<span>'0'</span> == <span>''</span>)</span>; <span>// false</span><span>var_dump(<span>0</span> == [])</span>; <span>// false</span></code>
Best practice
==
and ===
is that the latter performs type equality judgment at the same time The above introduces PHP equality comparison and empty, isset, isnull, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.