Home  >  Article  >  Backend Development  >  PHP empty() isset() is_null() Differences and Performance Comparison_PHP Tutorial

PHP empty() isset() is_null() Differences and Performance Comparison_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:00:051362browse

In php, the three functions empty() isset() is_null() are all used to determine whether it is empty. However, if I want to understand these three functions in detail, I will find that there are still many differences. Below I Let me summarize it for you.

is_null(), empty(), isset(), these functions and == ", == array() are often used in actual operations. Because the functions are very similar, their differences may be overlooked , if you are not careful, you will bring a lot of trouble to your work. These structures are listed below for your own and everyone’s reference. In view of the accuracy of the expression, some explanations are from the original English manual to avoid untimely updates of the Chinese manual. and issues such as improper translation

.

is_null()
is_null(), bool, when the parameters meet the three conditions of null, is_null() will return TRUE.

null type, the following situations will be considered NULL:

it has been assigned the constant NULL.

it has not been set to any value yet.

it has been unset().

source:http://cn2.php.net/manual/en/language.types.null.php

isset()
isset(), bool, used to determine whether the parameter is set and not NULL. Parameters can only be variables.

If the variable is not set, or the variable is unset()ed, or the variable value is NULL, return FALSE, otherwise return TRUE. That is, if it is not NULL, it falls into the category of isset, which is exactly the opposite of the is_null() function.

If multiple parameters are passed, the intersection will be taken. That is, TRUE is returned only when all parameters are consistent with isset().

ps:defined(), bool, used to check whether the constant is set.

source:http://cn2.php.net/manual/en/function.isset.php

empty()
empty(), bool, is mainly used to determine whether a variable is empty. Parameters can only be variables.

The following situations will be deemed empty:

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

“” (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

“0″ (0 as a string)

NULL

FALSE

array() (an empty array)

var $var; (a variable declared, but without a value in a class)

“” (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

“0″ (0 as a string)

NULL

FALSE

array() (an empty array)
 代码如下 复制代码

$a;
$b = false;
$c = '';
$d = 0;
$e = null;
$f = array();

?>

empty()

首先是empty的var_dump输出:


:
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)

从代码中可以看出,只要数据类型是否为空或假,empty()就输出true。
isset()

再看看isset的输出:
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)

可以看出isset()只能用来判断是否为NULL和未定义。
is_null()

最后是is_null的输出:
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)

var $var; (a variable declared, but without a value in a class)
Note: If the parameter is an unset variable, the variable will be considered NULL, no error will be reported, and TRUE will be returned. But note that after 5.0.0, Objects with no properties are no longer considered empty. source:http://cn2.php.net/manual/en/function.empty.php The other ways to determine whether it is empty are == ", == array(), etc., which are quite limited and have nothing to say. The types of tests are as follows:
The code is as follows Copy code
$a;<🎜> $b = false;<🎜> $c = '';<🎜> $d = 0;<🎜> $e = null;<🎜> $f = array();<🎜> <🎜>?> empty() First is the var_dump output of empty: : var_dump(empty($a));<🎜> var_dump(empty($b));<🎜> var_dump(empty($c));<🎜> var_dump(empty($d));<🎜> var_dump(empty($e));<🎜> var_dump(empty($f));<🎜> ?> The program output is: 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. isset() Look at the output of isset: 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. is_null() Finally is the output of is_null: 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.

Summary summary of the differences between isset, empty, and is_null:

What I just introduced: checking variables and parameter types is the basis of the differences between these three functions, and it is also the most easily overlooked. I saw many articles comparing these three functions on the Internet. These are rarely covered. What I want to talk about next is the difference when both check existing variables.

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

$a=100;

$b="";

$c=null;

//isset检查

echo "isset","$a=$a",isset($a)?"define":"undefine","rn";

echo "isset","$b=$b",isset($b)?"define":"undefine","rn";

echo "isset","$c=$c",isset($c)?"define":"undefine","rn";

unset($b);

echo "isset","$b",isset($b)?"define":"undefine","rn";

$b=0;

echo "rnrn";

 

//empty检查

echo "empty","$a=$a",!empty($a)?"no empty":"empty","rn";

echo "empty","$b=$b",!empty($b)?"no empty":"empty","rn";

echo "empty","$c=$c",!empty($c)?"no empty":"empty","rn";

unset($b);

echo "empty","$b",!empty($b)?"no empty":"empty","rn";

$b=0;

echo "rnrn";

 

//is_null检查

echo "is_null","$a=$a",!is_null($a)?"no null":"null","rn";

echo "is_null","$b=$b",!is_null($b)?"no null":"null","rn";

echo "is_null","$c=$c",!is_null($c)?"no null":"null","rn";

unset($b);

echo "is_null","$b",is_null($b)?"no null":"null","rn";

Copy code
$a=100;

$c=null; //isset check echo "isset","$a=$a",isset($a)?"define":"undefine","rn"; echo "isset","$b=$b",isset($b)?"define":"undefine","rn"; echo "isset","$c=$c",isset($c)?"define":"undefine","rn";
unset($b); echo "isset","$b",isset($b)?"define":"undefine","rn";
$b=0; echo "rnrn"; //empty check echo "empty","$a=$a",!empty($a)?"no empty":"empty","rn"; echo "empty","$b=$b",!empty($b)?"no empty":"empty","rn"; echo "empty","$c=$c",!empty($c)?"no empty":"empty","rn"; unset($b); echo "empty","$b",!empty($b)?"no empty":"empty","rn"; $b=0; echo "rnrn"; //is_null check echo "is_null","$a=$a",!is_null($a)?"no null":"null","rn"; echo "is_null","$b=$b",!is_null($b)?"no null":"null","rn"; echo "is_null","$c=$c",!is_null($c)?"no null":"null","rn"; unset($b); echo "is_null","$b",is_null($b)?"no null":"null","rn"; http://www.bkjia.com/PHPjc/631267.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631267.htmlTechArticleIn php, the three functions empty() isset() is_null() all determine whether it is empty. But if I want to understand these three functions in detail, I will find that there are still many differences. Below...
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