根据PHP.net,isset()执行以下操作:
Determine if a variable is set and is not NULL.
写作时:
您正在检查$_SESSION [‘username’]是否等于true.换句话说,您正在检查该值是否不等于false.
根据PHP.net,以下被认为是错误的:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
如您所见,未设置的变量/ NULL变量被视为FALSE.因此,通过测试$_SESSION元素是否为true,您还可以确定它是否存在.
另一方面,Isset实际检查变量是否存在.如果您想知道该名称是否存在SESSION变量,请使用isset()作为测试它的TRUE / FALSE不依赖于变量是否存在.
此外,请看以下示例:
$_SESSION['a'] = FALSE;
if($_SESSION['a']){
echo 'Hello'; //This line is NOT echo'd.
}
if(isset($_SESSION['b'])){
echo 'Hello'; //This line is NOT echo'd because $_SESSION['b'] has not been set.
}