如何检查PHP中是否设置了变量
答案:使用 PHP isset()
函数
您可以使用 PHP isset()
函数来测试是否设置了变量。 如果测试已设置为 NULL
的变量,isset()
将返回 FALSE
。
让我们看一个例子来了解这个函数的基本工作原理:
<?php
$var1 = '';
if(isset($var1)){
echo 'This line is printed, because the $var1 is set.';
}
echo "<br>";
$var2 = 'Hello World!';
if(isset($var2)){
echo 'This line is printed, because the $var2 is set.';
}
echo "<br>";
// 取消设置变量
unset($var2);
if(isset($var2)){
echo 'This line is printed, because the $var2 is set.';
} else{
echo 'This line is printed, because the $var2 is not set.';
}
echo "<br>";
$var3 = NULL;
if(isset($var3)){
echo 'This line is printed, because the $var3 is set.';
} else{
echo 'This line is printed, because the $var3 is not set.';
}
?>
FAQ 相关问题解答
以下是与此主题相关的更多常见问题解答:
Advertisements