點擊藍字關注我們!每天獲取最新的編程小知識!
源 / php中文網 源 / www.php.cn
isset()函數是PHP中的內置函數,它檢查變量是否已設置且不為NULL。此函數還檢查聲明的變量,數組或數組鍵是否具有空值,如果是,isset()返回false,它在所有其他可能的情況下返回true。
語法:
bool isset( $var, mixed )
參數:此函數接受多個參數。這個函數的第一個參數是$ var。此參數用於存儲變量的值。
例:
<?php
$num = '0';
if( isset( $num ) ) {
print_r(" $num is set with isset function <br>");
}
$array = array();
echo isset($array['geeks']) ?
'array is set.' : 'array is not set.';
?>
輸出:0 is set with isset function
array is not set.
empty()函數是一種語言構造,用於確定給定變量是空還是NULL。!empty()函數是empty()函數的否定或補充。empty()函數與!isset()函數相當,而!empty()函數等於isset()函數。
例:
<?php
$temp = 0;
if (empty($temp)) {
echo $temp . ' is considered empty';
}
echo "\n";
$new = 1;
if (!empty($new)) {
echo $new . ' is considered set';
}
?>
輸出:
0 is considered empty
1 is considered set
檢查兩個函數的原因:
isset()和!empty()函數類似,兩者都將返回相同的結果。但唯一的區別是!當變量不存在時,empty()函數不會生成任何警告或電子通知。它足以使用任何一個功能。通過將兩個功能合併到程序中會導致時間流逝和不必要的內存使用。
例:
<?php
$num = '0';
if( isset ( $num ) ) {
print_r( $num . " is set with isset function");
}
echo "\n";
$num = 1;
if( !empty ( $num ) ) {
print_r($num . " is set with !empty function");
}
輸出:0 is set with isset function
1 is set with !empty function
相關推薦:《PHP教程》
http://www.php.cn/course/list/29.html
本篇文章就是為什麼要在PHP中同時檢查isset()和!empty()函數的原因介紹,希望對需要的朋友有所幫助!
-END-
聲明:本文選自「 php中文網 」,搜索「 phpcnnew 」即可關注!