background image

//OK

isset(NULL);

//PHP Parse error: syntax error

?>
 
说了这么多

isset 的缺点了, 说点它的优点吧:

因为

isset 是语句, 所以它快!

在一千万次的简单检测语句循环中

, 对比结果如下:

代码如下

:

 
<?php
$a="laruence":
isset($a); 

//用时: 1.15s

is_null($a); 

//用时: 3.89s

?>
 
因为

isset 叫做 isset, 所以它在检测未定义变量的时候, 不会产生 NOTICE:

代码如下

:

 
<?php
isset($laruence);

//OK

is_null($laruence);

//PHP Notice: Undefined variable: laruence

?>
 
那么

, 对于什么时候用 isset 什么时候用 is_null, 我有什么建议呢?

, 我的建议是, 用函数做函数应该做的事情~, 听起来象废话?

isset => is set? => 变量有没有被赋值(声明)
is_null => is 

null

? => 变量为 NULL 么?

另外

, 如果要用 is_null, 我建议使用 “=== NULL” 来代替, 它不仅语义和 is_null 一致, 

结果一致

, 速度还和 isset 差不多:

在一千万次的简单检测语句循环中

, 对比结果如下:

代码如下

:

 
<?php
$a="laruence":
isset($a); 

//用时: 1.15s

is_null($a); 

//用时: 3.88s

$a===NULL; 

//用时: 1.22s

?>