Home > Article > Backend Development > php empty(), isset(), is_null() function usage examples_PHP tutorial
In php, the three functions empty(), isset(), and is_null() are similar to many friends, but if we think about it briefly, they seem to be different. One is a null value, one is a variable, and whether it is null. Below I Let’s give an example to illustrate.
Many people are vague about the usage of PHP's empty(), isset() and is_null() functions. There is a lot of information on the Internet, but they may not be able to explain it clearly. Here is a test example, which is helpful for understanding this. The distinction between the three functions can be of great benefit.
The types of tests are as follows:
$a;
$b = false;
$c = '';
$d = 0;
$e = null;
$f = array();
?>
empty()
The code is as follows | Copy code | ||||||||
var_dump(empty($a)); var_dump(empty($b));
var_dump(empty($c));
var_dump(empty($d));
var_dump(empty($e));
// Output bool(true)
bool(true) |
As can be seen from the code, empty() outputs true as long as the data type is empty or false.
代码如下 | 复制代码 |
$id=0; |
isset()
The code is as follows | Copy code | ||||
|
The code is as follows | Copy code |
$id=0; empty($id)?print "It's empty .":print "It's $id ."; //Result: It's empty . Print " "; !isset($id)?print "It's empty .":print "It's $id ."; //Result: It's 0 . |
The code is as follows | Copy code |
if(empty($id)) $id=1; - If id=0, id will also be 1 if(!isset($id)) $id=1; - If id=0, id will not be 1 |
The above inference can be detected by running the following code separately:
The code is as follows | Copy code | ||||
if(empty($id)) $id=1; Print $id; // get 1
if(!isset($id)) $id=1;
print $id; //get 0 |
代码如下 | 复制代码 |
var_dump(is_null($a)); // 输出 |
The code is as follows | Copy code |
var_dump(is_null($a)); var_dump(is_null($b)); var_dump(is_null($c)); var_dump(is_null($d)); var_dump(is_null($e)); var_dump(is_null($f)); ?> // Output bool(true) bool(false) bool(false) bool(false) bool(true) bool(false) |
is_null is literal.
It can be seen that empty() can be used to determine whether all data types are empty or false, while is_null is basically the same as isset and can only be used to determine whether it is NULL and undefined.
I will attach a table for you later
The following table can clearly illustrate the relationship between them:
Variable is_null isset
$a=”” true
$a=null because
var $a because
$a=array() because
$a=false “
$a=15 Since
$a=1 because false
$a=0 because
$a="0" true
$ a = "True" False False True
$ a = "False" False False True
http://www.bkjia.com/PHPjc/445604.html