doubledee Posted March 19, 2012 Share Posted March 19, 2012 What is the difference between... $_SESSION['memberID']) = ''; and unset($_SESSION['memberID']); And why would I want to use one versus the other? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259289-clearing-out-session/ Share on other sites More sharing options...
smerny Posted March 19, 2012 Share Posted March 19, 2012 unset actually unsets the session variable.. the other way you still have the session variable there, it's just set to an empty string. should use unset Quote Link to comment https://forums.phpfreaks.com/topic/259289-clearing-out-session/#findComment-1329185 Share on other sites More sharing options...
salathe Posted March 19, 2012 Share Posted March 19, 2012 They're generally both used to say, "hey, there's nothing useful in this session value any more." Which one you use might be determined by the code being used to check if there is a useful value to be had. For example, one script might simply check for the presence of a particular key in the $_SESSION array; if it's there then the author might have chosen to assume there is an okay value for use when he needs it. Another script might prefer to keep that key present in the array but with a specific value to raise the same flag as the other script did by removing the key entirely. if (array_key_exists('blah', $_SESSION)) // versus if ($_SESSION['blah'] !== '') I wouldn't stress over which to choose, so long as it ties in with what your script is expecting of an empty/missing/unset session value. Quote Link to comment https://forums.phpfreaks.com/topic/259289-clearing-out-session/#findComment-1329191 Share on other sites More sharing options...
scootstah Posted March 19, 2012 Share Posted March 19, 2012 Hopefully this illustrates the difference... $foo['bar'] = 'bleh'; var_dump($foo); // array(1) { ["bar"]=> string(4) "bleh" } $foo['bar'] = ''; var_dump($foo); // array(1) { ["bar"]=> string(0) "" } unset($foo['bar']); var_dump($foo); // array(0) { } Quote Link to comment https://forums.phpfreaks.com/topic/259289-clearing-out-session/#findComment-1329230 Share on other sites More sharing options...
SaCH Posted March 20, 2012 Share Posted March 20, 2012 That means we should use the function unset() to clear the session data. Else if we simply set it like this $_SESSION['value'] = ''; rather than using unset() function it will consider there is a session value existing. We can understand this thing from "scootstah" post. Quote Link to comment https://forums.phpfreaks.com/topic/259289-clearing-out-session/#findComment-1329264 Share on other sites More sharing options...
doubledee Posted March 20, 2012 Author Share Posted March 20, 2012 I assumed that unset() was more thorough. Appears to be a right assumption, and probably better for me. Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259289-clearing-out-session/#findComment-1329268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.