views:

113

answers:

2

hi i tried 2 things what should be the same however it my testing says different does anyone know why the only thing i do is put it in a variable...

    if ($_SESSION[$something] === null)
        echo("this is null");

$_SESSION[$something] does not exists so it indeed says: "this is null". now look at this

$theSession = $_SESSION[$something];

if ($theSession === null)
    echo("this is null");

now it does not say "this is null" while it should be exactly the same right?

+3  A: 

You need a $ in front of theSession in the second block of code.

You do may not need the $ in front of something. You only need it if $something is holding a string of the session variable name. Otherwise if something is the session variable name you dont need the $.

Josh Curren
Most likely the $ in $something _is_ useful, like in `$something='xyz'; echo $_SESSION[$something]; `
VolkerK
oh.. true i didnt think of that...
Josh Curren
also if you get rid of the $ you need to quote the string
Tom Haigh
A: 

You should also consider using is_null to check is a variable contains a null value.

John Conde
Any particular reason? The comments on that documentation page say it's slower (by a completely unimportant margin, but still).
nickf
Readability primarily. if (is_null($$something)) { ... is as about as straightforward as it gets.
John Conde