Re: deleting $_SESSION

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jochem Maas wrote:
AndreaD wrote:

I have a session variable called

$_SESSION['total'] the problem is I can't delete/reset it. I have tried

$_SESSION['total']= 0;

$_SESSION['total']= "";


I guess you didn't know/think of trying null:

$_SESSION['total'] = null;

...which has the same effect as using:

unset($_SESSION['total']); // as you have been told on at least 3 diff occasions ;-)

this oneliner might give you a little insight:
<?php $x=1;$y=2;unset($x);$y=null;var_dump($x,isset($x),empty($x),$y,isset($y),empty($y)); ?>


I encourage you to read the following pages for a better understanding of variables:
http://php.net/unset http://php.net/empty http://php.net/isset,
and this section: http://php.net/variables


have fun!
rgds,
Jochem

PS - personally I try to remember to set a var to null if the code is inside a loop,
in order to minimize function call overhead. (anyone with real skill care to comment
as to whether there is actually a difference between setting a var to null and calling unset
on it? also speedwise?)

If you set a variable to null, it will be null. If it's unset, it no longer exists, it's not even a null. But will evaluate to null if used and notice will be generated.


This 7-liner will ilustrate it:

error_reporting(E_ALL);
$null = $unset = 0; // to get started
var_dump($null, $unset);
$null = null;
unset($unset);
var_dump($null, $unset); // both are nulls, but notice is generated
var_dump(array_key_exists('null', $GLOBALS), array_key_exists('unset', $GLOBALS)); // 'unset' is no longer in $GLOBALS


Speed? I don't think there is much difference. But I would use unset() to get rid of unused variables as they are pesistent.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux