Home  >  Article  >  Backend Development  >  Simple comparison of is_null($x) and $x === null in PHP

Simple comparison of is_null($x) and $x === null in PHP

青灯夜游
青灯夜游Original
2019-02-27 10:46:364066browse

In PHP, both is_null($x) and $x === null can be used to determine whether the value of variable $x is "NULL", so what is the difference between them? The following article will introduce you to the is_null() and === null methods, and briefly compare these two methods. I hope it will be helpful to everyone.

Simple comparison of is_null($x) and $x === null in PHP

is_null() function

is_null() is a built-in function in PHP for Find if a variable is NULL. Returns True if the given variable is null, False otherwise.

Note: Null is a special data type in PHP and can only have one NULL value. A variable of data type NULL represents a variable that has not yet been assigned a value. Any variable can be made null by setting its value to NULL.

Basic sentence structure:

is_null($var)

Example:

<?php 
$array =array(false, NULL, 15, 0 ); 
for($i=0;$i<4;$i++){
var_dump(is_null($array[$i])); 
}
?>

Output:

Simple comparison of is_null($x) and $x === null in PHP

=== null

=== null is the same comparison operator. If the value on the left side of "===" is equal to null, return true, otherwise Return false. Example: $x === null, if the value of $x is equal to NULL, return true.

Basic sentence structure:

$var === null

Example:

<?php 
$array =array(false, NULL, 15, 0 ); 
for($i=0;$i<4;$i++){
if ($array[$i] === null) {
echo "True <br>"; 
}
    else 
    echo "False <br>"; 
}
?>

Output:

Simple comparison of is_null($x) and $x === null in PHP

Conclusion:

is_null() is a function, and === null is a comparison operator. Therefore, is_null() is slightly slower (function call overhead), but is_null() can be used as a callback. Which method to choose to determine whether the value is "null" needs to be based on actual needs.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of Simple comparison of is_null($x) and $x === null in PHP. For more information, please follow other related articles on the PHP Chinese website!

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