Difference between `if (isset($_SESSION))` and `if ($_SESSION)`?
Could someone explain the difference when checking if a variable is set (that’s what I’d be using it for)?
Apart from the possible debug notice, the first also checks for booleanes of the contained value, not just presence.
3 Answers 3
In PHP, if a variable does not exist (is «unset») then PHP will spit out an E_NOTICE error, create the missing variable, and assign it to NULL .
If you do not want your scripts filled with annoying error messages that leak information about your server and script — then use isset() to test before trying to access a value.
Basically, you’re asking PHP to get username out of $_SESSION and PHP is like, «there is no username «!
My guess would be the author of the code above (without the isset()) has added the variable to _SESSION and «knows» (hah!) that it will be found later.
Thanks! So would using if($var) be more useful if, say, you only want to check if it is not ‘0’ or blank?
@DankPiff, basically, if you know beyond a shadow of a doubt, that the variable exists (because you just made it) then you can use it directly like if($var) . If you are unsure, then you need to test first with if(isset($var)) or if(empty($var)) so PHP knows that missing values are ok and you don’t need a nasty error message.
According to PHP.net, isset() does the following:
Determine if a variable is set and is not NULL.
You are checking to see if $_SESSION[‘username’] is equal to true. In other words, you are checking if the value does not equal false.
According to PHP.net the following are considered 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 testing if the $_SESSION element is true, you are also determining if it exists.
Isset, on the otherhand, actually checks if the variable exists. If you would like to know if there is a SESSION variable by that name, use isset() as testing it for TRUE/FALSE isn’t dependent on whether the variable exists or not.
Further, look at 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. >
PHP session false not set
When setting false value to session, the session is set, but the value is empty. The type is boolean. This code:
"; echo "IsSet2: " . isset($_SESSION["IsMobile2"]) . "; IsMobile2: " . $_SESSION["IsMobile2"] . ";
"; ?>
IsSet1: 1; IsMobile1: ; type: boolean; IsSet2: 1; IsMobile2: 1;
but of course it is not working, because IsMobile is empty boolean. (In original I just use IsMobile. IsMobile1 and IsMobile2 is just for testing).
is_bool tells you that you’re dealing with a boolean. It doesn’t check to see whether that variable is true or false.
True. I meant filter_var not is_bool and filter_var(», FILTER_VALIDATE_BOOLEAN); return false which is expected behaviour. From: stackoverflow.com/questions/7336861/…
btw isset() will return false if the array index exists and has a value set, but the value is null. Alternatively you can use array_key_exists() if you just want to check if a key exists. If this code might run without the $_SESSION variable existing at all in some cases, you could combine the two by checking isset($_SESSION) before calling array_key_exists() .
3 Answers 3
I have faced this issue myself many times and, while still unconfirmed, I have come to this conclusion:
As boolean type can’t be used as constant for 1 or 0, it just happens so that it is cast to int upon printing or any other output attempt. But it only happens to the TRUE . FALSE always (or at least in most cases) stays empty (I’ve never seen a bool var output FALSE ).
But the good news in, if you use a variable defined as FALSE in an if statement, it will be interpreted as FALSE (as expected).
So, what to do if you want to output them?
if ($_SESSION["IsMobile"] == FALSE)
UPDATE Oh yeah, here’s what I missed in the docs:
A boolean TRUE value is converted to the string «1». Boolean FALSE is converted to «» (the empty string). This allows conversion back and forth between boolean and string values.
What Booleans params in session_regenerate_id() does?
delete_old_session Whether to delete the old associated session file or not. So basically when we pass True to session_regenerate_id() , it will delete the old Session values associated with this ? So I tried to test this with the below lines of codes,
'; $_SESSION['test'] = 'ok'; //set a session value session_regenerate_id(false); // true or False what difference does it make ? echo 'New Session ID with FALSE param during regenaration ::'.session_id().'
'; echo 'IS SESSION EXIST ?
'; print_r($_SESSION); echo '
'; session_regenerate_id(true); // true or False what difference does it make ? echo 'New Session ID with TRUE param during regenaration ::'.session_id().'
'; echo 'IS SESSION EXIST ?
'; print_r($_SESSION); echo '
'; exit;
Initial Session ID ::2du1rt3c5ou01db2ruvr32qli7 New Session ID with FALSE param during regenaration ::epid6rhi2k9cfthdrh69udqlk1 IS SESSION EXIST ? Array ( [test] => ok ) New Session ID with TRUE param during regenaration ::4o766kh2g4eae8vlqupnkmmer5 IS SESSION EXIST ? Array ( [test] => ok )
In my case I can see True or False doesn’t change the behavior of session values that are set , it only changes the id of session . Is something wrong with my code or Understanding ? Edit 2 : And what is the proper way if I want to initialize a session (first time when user visits) with my own session id( custom generated) ? Something like this approach ?
session_start(); //Create new session without destroying the old one session_regenerate_id(false); //Grab current session ID and close both sessions to allow other scripts to use them $newSession = session_id(); session_write_close(); // Set session ID to the new one, and start it back up again session_id($newSession); session_start();