Something strange: NULL is not resulting in !isset() test!

Hello,

I am setting: $_SESSION[‘buy_amount’] = NULL

This should result in $_SESSION[‘buy_amount’] as not being set! Right?
But when I test for it to be !isset it is saying that it is SET!
I mean of course:

if (isset($_SESSION[‘buy_amount’])) {
echo ‘<p>buy_amount_session ISSET’;
}

What the HEK is going on?
Why is this $_SESSION[‘buy_amount’] after being set to NULL still saying that it isset?

Thanks

!isset and NULL are not equivalent as you just found out. If you wish to unset a session variable, use unset($_SESSION[‘buy_amount’]);

isset() will return FALSE if testing a variable that has been set to NULL

http://us2.php.net/manual/en/function.isset.php

Check your code. This works as expected:


$_SESSION['buy_amount'] = NULL;
if (isset($SESSION['buy_amount'])) echo "buy_amount is SET\
";
else                               echo "buy_amount IS NOT SET\
";

Actually in subsequent test $_SESSION[‘buy_amount’] = NULL is leading to

if (isset($_SESSION[‘buy_amount’])) is NOT being true, as it should be.
SO I have no idea why earlier this basic test was failing! I think I need to take a brake :slight_smile:

But it is good to get the tip from you about unset($_SESSION[‘buy_amount’]).

However, unset($_SESSION[‘buy_amount’]) should be same as $_SESSION[‘buy_amount’] = NULL

Ah, sorry, I miss read your question and confused myself. So in your case, setting it to NULL you expecting isset($_SESSION[‘buy_amount’]) to return false. However, it is returning true… time to run some tests, as I’m intrigued now :slight_smile:

Ah bummer, seems to be working fine for me…

<?php
session_start();

$_SESSION['my_value'] = "my value";
var_dump($_SESSION, isset($_SESSION['my_value']));

$_SESSION['my_value'] = NULL;
var_dump($_SESSION, isset($_SESSION['my_value']));

Produces:

array (size=1)
  'my_value' => string 'my value' (length=8)
boolean true

array (size=1)
  'my_value' => null
boolean false

Only as far as isset is concerned. Setting buy_amount to null does not remove it from the array like unset does.

So then you agree as per your sample code that:

$_SESSION[‘my_value’] = “my value”;
and then
$_SESSION[‘my_value’] = null;

will result in same thing as unset($_SESSION[‘my_value’]);

right?

As far as isset() is concerned, yes. Any other way, no. As the array key would still be part of $_SESSION using = null, but that wouldn’t be the case using unset.