Home >Backend Development >PHP Tutorial >PHP equality comparison and empty, isset, isnull

PHP equality comparison and empty, isset, isnull

WBOY
WBOYOriginal
2016-07-29 08:59:031337browse

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>
  • null is compared with the null value through ==. 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

  • empty: Check whether a variable is empty
<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>
  • isset: Return TRUE if the variable var exists and the value is not NULL, otherwise return FALSE.
<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>
  • isnull: Determine whether the variable is null, equivalent to === 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

  • Use empty to judge null values;
  • isset cannot judge variables that exist but have a null value; The difference between
  • php == and === is that the latter performs type equality judgment at the same time
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn