439   PHP

一,isset()

Determine if a variable is set and is not NULL

判断一个变量是否被定义而且不为NULL,否则返回false
测试代码:

$var_has_set_and_not_null="";
echo "\n-----var_has_set_and_not_null--------\n";
var_dump(isset($var_has_set_and_not_null));
echo "\n-------------\n";

$var_has_set_but_null=NULL;
echo "\n-----var_has_set_but_null--------\n";
var_dump(isset($var_has_set_but_null));
echo "\n-------------\n";


echo "\n-----var_not_set--------\n";
var_dump(isset($var_not_set));
echo "\n-------------\n";


C:\Users\luckybird\Desktop\test>php test.php

-----var_has_set_and_not_null--------
bool(true)

-------------

-----var_has_set_but_null--------
bool(false)

-------------

-----var_not_set--------
bool(false)

-------------

二,empty()

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

“” (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; (a variable declared, but without a value)

Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

判断变量是否为空,空的定义如下:
“”:空字符串;
0:整数0
0.0:浮点数0.0
“0”:字符串”0″
NULL:NULL值;
FALSE:FALSE值;
array():空数组;
$var; 变量定义了但没有赋值;

特别注意:
PHP5.5之后empty()函数只支持变量判断,其他形式就会抛出语法错误;

测试代码:


$empty_string="";
echo "\n-----empty_string--------\n";
var_dump(empty($empty_string));
echo "\n-------------\n";

$zero_integer=0;
echo "\n-----zero_integer--------\n";
var_dump(empty($zero_integer));
echo "\n-------------\n";

$zero_float=0.0;
echo "\n-----zero_float--------\n";
var_dump(empty($zero_float));
echo "\n-------------\n";

$null=null;
echo "\n-----null--------\n";
var_dump(empty($null));
echo "\n-------------\n";

$false=false;
echo "\n-----false--------\n";
var_dump(empty($false));
echo "\n-------------\n";

$empty_array=array();
echo "\n-----empty_array--------\n";
var_dump(empty($empty_array));
echo "\n-------------\n";

$no_value;
echo "\n-----no_value--------\n";
var_dump(empty($no_value));
echo "\n-------------\n";




C:\Users\luckybird\Desktop\test>php test.php

-----empty_string--------
bool(true)

-------------

-----zero_integer--------
bool(true)

-------------

-----zero_float--------
bool(true)

-------------

-----null--------
bool(true)

-------------

-----false--------
bool(true)

-------------

-----empty_array--------
bool(true)

-------------

-----no_value--------
bool(true)

-------------

-----not_set--------
bool(true)
-------------

三,is_null

Returns TRUE if var is null, FALSE otherwise.

判断变量是否为null,否则返回false

测试代码:


$is_null=null;
echo "\n-----is_null--------\n";
var_dump(is_null($is_null));
echo "\n-------------\n";

$not_null="";
echo "\n-----not_null--------\n";
var_dump(is_null($not_null));
echo "\n-------------\n";



C:\Users\luckybird\Desktop\test>php test.php

-----is_null--------
bool(true)

-------------

-----not_null--------
bool(false)

-------------

四,三个函数之间的关联和区别

isset和is_null:
1,isset检查变量是否被定义,而且不为null;is_null检查是否null;所以isset检查已经包含了is_null了;
2,如果变量没定义而使用is_null判断,会抛出语法错误,所以建议使用isset检查变量吧。

测试代码:


$is_null=null;
echo "\n-----is_null--------\n";
echo "\n------isset-------\n";
var_dump(isset($is_null));
echo "\n-------------\n";
echo "\n------is_null-------\n";
var_dump(is_null($is_null));
echo "\n-------------\n";


$not_null="";
echo "\n-----not_null--------\n";
echo "\n------isset-------\n";
var_dump(isset($not_null));
echo "\n-------------\n";
echo "\n------is_null-------\n";
var_dump(is_null($not_null));
echo "\n-------------\n";


echo "\n-----not_set--------\n";
echo "\n------isset-------\n";
var_dump(isset($not_set));
echo "\n-------------\n";
echo "\n------is_null-------\n";
var_dump(is_null($not_set));
echo "\n-------------\n";


C:\Users\luckybird\Desktop\test>php test.php

-----is_null--------

------isset-------
bool(false)

-------------

------is_null-------
bool(true)

-------------

-----not_null--------

------isset-------
bool(true)

-------------

------is_null-------
bool(false)

-------------

-----not_set--------

------isset-------
bool(false)

-------------

------is_null-------

Notice: Undefined variable: not_set in C:\Users\luckybird\Desktop\test\test.php on line 28
bool(true)

-------------

empty和is_null
1,empty检查变量中也包括了is_null的null判断

测试代码:

$is_null=null;
echo "\n-----is_null--------\n";
echo "\n------empty-------\n";
var_dump(empty($is_null));
echo "\n-------------\n";
echo "\n------is_null-------\n";
var_dump(is_null($is_null));
echo "\n-------------\n";


$not_null="";
echo "\n-----not_null--------\n";
echo "\n------empty-------\n";
var_dump(empty($not_null));
echo "\n-------------\n";
echo "\n------is_null-------\n";
var_dump(is_null($not_null));
echo "\n-------------\n";


echo "\n-----not_set--------\n";
echo "\n------empty-------\n";
var_dump(empty($not_set));
echo "\n-------------\n";
echo "\n------is_null-------\n";
var_dump(is_null($not_set));
echo "\n-------------\n";


C:\Users\luckybird\Desktop\test>php test.php

-----is_null--------

------empty-------
bool(true)

-------------

------is_null-------
bool(true)

-------------

-----not_null--------

------empty-------
bool(true)

-------------

------is_null-------
bool(false)

-------------

-----not_set--------

------empty-------
bool(true)

-------------

------is_null-------

Notice: Undefined variable: not_set in C:\Users\luckybird\Desktop\test\test.php on line 28
bool(true)

-------------

isset和empty

1,isset和empty都有null的判断,没定义的变量也会默认为null,换句话说empty检查包含了isset;
2,empty还多了其他空的判断,比如整数0,浮点数0.0,以及空字符串,空数组等;

测试代码:



$is_null=null;
echo "\n-----is_null--------\n";
echo "\n------empty-------\n";
var_dump(empty($is_null));
echo "\n-------------\n";
echo "\n------isset-------\n";
var_dump(isset($is_null));
echo "\n-------------\n";


$not_null="";
echo "\n-----not_null--------\n";
echo "\n------empty-------\n";
var_dump(empty($not_null));
echo "\n-------------\n";
echo "\n------isset-------\n";
var_dump(isset($not_null));
echo "\n-------------\n";


echo "\n-----not_set--------\n";
echo "\n------empty-------\n";
var_dump(empty($not_set));
echo "\n-------------\n";
echo "\n------isset-------\n";
var_dump(isset($not_set));
echo "\n-------------\n";


C:\Users\luckybird\Desktop\test>php test.php

-----is_null--------

------empty-------
bool(true)

-------------

------isset-------
bool(false)

-------------

-----not_null--------

------empty-------
bool(true)

-------------

------isset-------
bool(true)

-------------

-----not_set--------

------empty-------
bool(true)

-------------

------isset-------
bool(false)

-------------


五,总结:
isset,empty,is_null各有自己的检查规则,同时互相之间会有包含,但是不是相等,要根据实际需求来选用。




Leave a Reply

Your email address will not be published. Required fields are marked *