Difference between `if (isset($_SESSION))` and `if ($_SESSION)`?
Asked Answered
L

3

7

I've noticed that frequently people simply write

<?php  if($_SESSION['username']) {...} ?>

while I have been using:

 <?php if(isset($_SESSION['username'])) {...} ?> 

Could someone explain the difference when checking if a variable is set (that's what I'd be using it for)?

Laurice answered 7/5, 2012 at 23:50 Comment(1)
Apart from the possible debug notice, the first also checks for booleanes of the contained value, not just presence.Voltaic
H
8

According to PHP.net, isset() does the following:

Determine if a variable is set and is not NULL.

When writing:

<?php  if($_SESSION['username']) {...} ?>

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.
}
Hluchy answered 8/5, 2012 at 0:17 Comment(0)
H
10

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"!

Himation answered 7/5, 2012 at 23:51 Comment(4)
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.Ulrikaumeko
Besides those E_NOTICE errors are useful in development as they identify typos in variable names.Peridium
Thanks! So would using if($var) be more useful if, say, you only want to check if it is not '0' or blank?Laurice
@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.Himation
H
8

According to PHP.net, isset() does the following:

Determine if a variable is set and is not NULL.

When writing:

<?php  if($_SESSION['username']) {...} ?>

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.
}
Hluchy answered 8/5, 2012 at 0:17 Comment(0)
F
3

say you set a variable = to false...

$variable = false;

This will not return anything, because it is making sure that the variable is not null, false or empty('')...

if($variable){
    echo'something';
}

This will work regardless of what we set the variable to, as long as we set it... It can be false, str, int, anything except null!

if(isset($variable)){
    echo'something';
}
Flamenco answered 8/5, 2012 at 0:5 Comment(2)
So using if($variable) is useful when you only want to check if it is false, or empty, while isset is useful when the contents of the variable may be '0' or even set to blank?Laurice
I generally only use the isset for checking if a variable has been submitted, like checking if a form has been submitted...Flamenco

© 2022 - 2024 — McMap. All rights reserved.