欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php empty,isset,is_null判斷比較(差異與異同)

 更新時(shí)間:2010年10月19日 02:30:20   作者:  
做php開發(fā)時(shí)候,想必在使用:empty,isset,is_null 這幾個(gè)函數(shù)時(shí)候,遇到一些問(wèn)題。甚至給自己的程序帶來(lái)一些安全隱患的bug。很多時(shí)候,對(duì)于isset,empty都認(rèn)為差不多。因此開發(fā)時(shí)候,就沒(méi)有注意,一段作為流程判斷時(shí)候,就出現(xiàn)bug問(wèn)題了。
一、舉例說(shuō)明
A.一個(gè)變量沒(méi)有定義,我們?cè)撛趺礃尤ヅ袛嗄?
復(fù)制代碼 代碼如下:

<?php
#不存在$test 變量

$isset= isset($test)?"test is define!":"test is undefine!";
echo "isset:$isset\r\n";

$empty=!empty($test)?"test is define!":"test is undefine!";
echo "empty:$empty\r\n";

$is_null=is_null($test)?"test is define!":"test is undefine!";
echo "is_null:$is_null\r\n";

測(cè)試結(jié)果是:

結(jié)果出來(lái)了:empty,isset首先都會(huì)檢查變量是否存在,然后對(duì)變量值進(jìn)行檢測(cè)。而is_null 只是直接檢查變量值,是否為null,因此如果變量未定義就會(huì)出現(xiàn)錯(cuò)誤!

B、看下各自接收的參數(shù)是什么?

isset函數(shù)參數(shù):

<?php
$test=100;
echo isset($test),isset(100),$isset($b=100);

 

<br />
<b>Parse error</b>:  parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in <b>PHPDocument3</b> on line <b>3</b><br />

empty函數(shù)參數(shù):

<?php
$test=100;

echo empty($test),empty(100),empty($b=100);

 

<br />
<b>Parse error</b>:  parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in <b>PHPDocument3</b> on line <b>3</b><br />

is_null函數(shù)參數(shù):

<?php
$test=100;

echo is_null($test),is_null(100),is_null($b=100);

運(yùn)行結(jié)果:沒(méi)有任何錯(cuò)誤。

比較結(jié)果出來(lái)了:empty,isset輸入?yún)?shù)必須是一個(gè)變量(php變量是以$字符開頭的),而is_null輸入?yún)?shù)只要是能夠有返回值就可以。(常量,變量,表達(dá)式等)。在php手冊(cè)里面,對(duì)于他們解析是:empty,isset 是一個(gè)語(yǔ)言結(jié)構(gòu)而非函數(shù),因此它無(wú)法被變量函數(shù)調(diào)用。

二、概括總結(jié)isset,empty,is_null區(qū)別:
剛才介紹的:檢查變量,以及參數(shù)類型,這個(gè)是這3個(gè)函數(shù)不同之處的基礎(chǔ),也是最容易被忽視的??吹骄W(wǎng)上有很多對(duì)這個(gè)3個(gè)函數(shù)進(jìn)行比較文章。很少涉及這些。下面我要說(shuō)的,是在都檢查已存在變量情況下,不同之處。

實(shí)例:

復(fù)制代碼 代碼如下:

<?php
$a=100;
$b="";
$c=null;
//isset檢查
echo "isset","\$a=$a",isset($a)?"define":"undefine","\r\n";
echo "isset","\$b=$b",isset($b)?"define":"undefine","\r\n";
echo "isset","\$c=$c",isset($c)?"define":"undefine","\r\n";
unset($b);
echo "isset","\$b",isset($b)?"define":"undefine","\r\n";
$b=0;
echo "\r\n\r\n";

//empty檢查
echo "empty","\$a=$a",!empty($a)?"no empty":"empty","\r\n";
echo "empty","\$b=$b",!empty($b)?"no empty":"empty","\r\n";
echo "empty","\$c=$c",!empty($c)?"no empty":"empty","\r\n";
unset($b);
echo "empty","\$b",!empty($b)?"no empty":"empty","\r\n";
$b=0;
echo "\r\n\r\n";

//is_null檢查
echo "is_null","\$a=$a",!is_null($a)?"no null":"null","\r\n";
echo "is_null","\$b=$b",!is_null($b)?"no null":"null","\r\n";
echo "is_null","\$c=$c",!is_null($c)?"no null":"null","\r\n";
unset($b);
echo "is_null","\$b",is_null($b)?"no null":"null","\r\n";


通過(guò)上面這個(gè)簡(jiǎn)單測(cè)試,我們可以大體知道,當(dāng)一個(gè)變量存在情況下:isset,empty,is_null檢測(cè),得到值情況了。上面沒(méi)有舉例更多變量。其實(shí)測(cè)試發(fā)現(xiàn):

empty

如果 變量 是非空或非零的值,則 empty() 返回 FALSE。換句話說(shuō),""、0、"0"、NULL、FALSE、array()、var $var、未定義; 以及沒(méi)有任何屬性的對(duì)象都將被認(rèn)為是空的,如果 var 為空,則返回 TRUE。

isset

如果 變量 存在(非NULL)則返回 TRUE,否則返回 FALSE(包括未定義)。變量值設(shè)置為:null,返回也是false;unset一個(gè)變量后,變量被取消了。注意,isset對(duì)于NULL值變量,特殊處理。

is_null

檢測(cè)傳入值【值,變量,表達(dá)式】是否是null,只有一個(gè)變量定義了,且它的值是null,它才返回TRUE . 其它都返回 FALSE 【未定義變量傳入后會(huì)出錯(cuò)!】.


疑問(wèn):怎么樣判斷一個(gè)變量被設(shè)置了,并且值為NULL呢?

通過(guò)上面比較,估計(jì)大家與我一樣,會(huì)有這個(gè)問(wèn)題浮現(xiàn)在腦海里面。 檢測(cè)一個(gè)變量是否是null 可以用:is_null,但如果變量未定義用它檢測(cè)會(huì)出錯(cuò)。因此,我們想到,檢測(cè)變量是否定義可以用:isset,但是如果一個(gè)變量值是:null, 則它會(huì)返回false . 哈哈,這個(gè)問(wèn)題怎么樣解決呢?等待大家分享……

檢查變量存在,并且值為NULL.
復(fù)制代碼 代碼如下:

<?php
function checkNull($a)
{
if(array_key_exists($a,$GLOBALS))
{
global $$a;
if(is_null($$a))
return true;
}
return false;
}
$test=null;
var_dump(checkNull("test"));
var_dump(checkNull("test1"));

相關(guān)文章

最新評(píng)論