php empty(), isset(), is_null() function usage examples_PHP tutorial

WBOY
Release: 2016-07-20 10:59:22
Original
911 people have browsed it

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));
var_dump(empty($f));
?>

// 输出
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true) 

var_dump(empty($a));

var_dump(empty($b));

var_dump(empty($c)); var_dump(empty($d)); var_dump(empty($e));
var_dump(empty($f));

?>
 代码如下 复制代码

var_dump(isset($a));
var_dump(isset($b));
var_dump(isset($c));
var_dump(isset($d));
var_dump(isset($e));
var_dump(isset($f));
?>

// 输出
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true) 

// Output

bool(true)

bool(true)
bool(true)
bool(true)
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;
    empty($id)?print "It's empty .":print "It's $id .";
      //结果:It's empty .
    print "
";
    !isset($id)?print "It's empty .":print "It's $id .";
      //结果:It's 0 .

isset()

The code is as follows Copy code
 代码如下 复制代码


if(empty($id)) $id=1; - 若 id=0 ,id 也会为1
if(!isset($id)) $id=1; - 若 id=0 ,id 不会为1

var_dump(isset($a)); var_dump(isset($b)); var_dump(isset($c)); var_dump(isset($d)); var_dump(isset($e)); var_dump(isset($f)); ?> // Output bool(false) bool(true) bool(true) bool(true) bool(false) bool(true)
It can be seen that isset() can only be used to determine whether it is NULL and undefined. Warning: isset() can only be used with variables, as passing any other arguments will cause a parsing error. If you want to check whether a constant has been set, you can use the defined() function. When you want to determine whether a variable has been declared, you can use the isset function When you want to determine whether a variable has been assigned data and is not empty, you can use the empty function When you want to determine whether a variable exists and is not empty, first use the isset function and then use the empty function For example, to detect the $id variable, when $id=0, use empty() and isset() to detect whether the variable $id has been configured. Both will return different values ​​- empty() considers it not configured, isset () can get the value of $id:
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 .
This means that when we use variable processing functions, when the variable may have a value of 0, we must be careful when using empty(). At this time, it is more sensible to replace it with isset. When the URL tail parameter of a php page appears id=0 (for example: test.php?id=0), try to compare:
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; // 得到 1

    if(!isset($id)) $id=1;
print $id; //得到 0

if(empty($id)) $id=1;

Print $id; // get 1

if(!isset($id)) $id=1; print $id; //get 0

 代码如下 复制代码

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));
?>

// 输出
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false) 

is_null()
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

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/445604.htmlTechArticleIn php, the three functions empty(), isset() and is_null() are similar to many friends. , but if we think about it briefly, it seems to be different. A null value, whether a is a variable, whether it is n...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template