php null详解,PHP is_null 用法 手册 | 示例代码

本文探讨了PHP中is_null()和isset()两个函数的区别。is_null()检查变量是否为NULL,而isset()则检测变量是否存在。它们返回值相反,且is_null()在变量未设置时会抛出Notice。在使用时,isset()作为语言构造比is_null()更高效,更适合检查变量的存在,除非你需要检查函数结果是否为NULL。
摘要由CSDN通过智能技术生成

For what I realized is that  is_null($var)  returns exactly the opposite of  isset($var) , except that is_null($var) throws a notice if $var hasn't been set yet.

the following will prove that:

$quirks= array(null,true,false,0,1,''," ","unset");

foreach($quirksas$var) {

if ($var==="unset") unset($var);

echois_null($var) ?1:0;

echo isset($var) ?1:0;

echo"n";

}?>

this will print out something like:

10    // null

01    // true

01    // false

01    // 0

01    // 1

01    // ''

01    // " "

Notice:  Undefined variable: var in /srv/www/htdocs/sandbox/null/nulltest.php on line 8

10    // (unset)

For the major quirky types/values is_null($var) obviously always returns the opposite of isset($var), and the notice clearly points out the faulty line with the is_null() statement. You might want to examine the return value of those functions in detail, but since both are specified to return boolean types there should be no doubt.

A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function.

isset() on the other hand is supposed to check for a VARIABLE's existence, which makes it a language construct rather than a function. Its sole porpuse lies in that checking. Passing anything else will result in an error.

Knowing that, allows us to draw the following unlikely conclusion:

isset() as a language construct is way faster, more reliable and powerful than is_null() and should be prefered over is_null(), except for when you're directly passing a function's result, which is considered bad programming practice anyways.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值