According to PHP.net, isset () does the following:
Determine if the variable is set and is not NULL.
When recording:
<?php if($_SESSION['username']) {...} ?>
You check to see if $ _SESSION ['username'] matches true. In other words, you are checking to see if the value is greater than false.
According to PHP.net , the following are FALSE:
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
As you can see, unset variables / NULL variables are considered FALSE. Therefore, by checking whether the $ _SESSION element is true, you also determine whether it exists.
Isset, on the other hand, actually checks if a variable exists. If you want to know if there is a SESSION variable with this name, use isset (), since checking it for TRUE / FALSE does not depend on whether or not the variable exists.
Next, consider the following examples:
$_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. }
source share