此問題僅供參考。如何在PHP中檢查變量是否已設置且is_null
我想知道如果有一種方法來檢查,如果一個變量is_set,如果是檢查是否被設置爲null
基礎上PHP type comparison tables不好像這是可能的。
例如,假設我們有這樣的代碼:
<?php
$x = null;
if(isset($x) && is_null($x)){
echo '$x is set to NULL';
}else{
echo '$x was never initialized';
}
echo "\n";
if(isset($y) && is_null($y)){
echo '$y is set to NULL';
}else{
echo '$y was never initialized';
}
echo "\n";
if(is_null($z)){
echo '$z is set to NULL';
}else{
echo '$z was never initialized';
}
?>
我期望的頁面顯示:
$x is set to NULL
$y was never initialized
$z is set to NULL <it should give an E_NOTICE>
但我正在逐漸
$x was never initialized
$y was never initialized
$z is set to NULL
我正在分配一個值,該值爲NULL,意味着'is_null($ x)'應該返回true以及'isset($ x)',因爲變量是SET且值爲NULL – Fabrizio