Home > Article > Backend Development > Simple comparison of is_null($x) and $x === null in PHP
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.
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:
=== 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:
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!