Jump to content

Looping through an unset statement


slanton

Recommended Posts

Really?
I wonder what the ...I am doing wrong
[code]
<?php
$_SESSION['test1']="test1";
$_SESSION['test2']="test2";

for($i=0;$i<14;$i++){
unset($_SESSION['test$i']);
}

echo $_SESSION['test1'];
echo $_SESSION['test2'];

?>
[/code]
Link to comment
Share on other sites

Hmm.. Let me try..

[b]Edit[/b]:
OMG >.< Now then I know it's not working in loop, or you wanna use a long way ?
[code]$_SESSION['test1'] = "";
$_SESSION['test2'] = "";[/code]

Hmm not sure this thread will helps anot, [a href=\"http://www.codecomments.com/message242702.html\" target=\"_blank\"]http://www.codecomments.com/message242702.html[/a]
Link to comment
Share on other sites

Your code will not work as PHP is unsetting a session var called 'test$i' it is treating your variable as-is meaning it is not swaping $i with its value.

So change your unset code to this:
[code]unset($_SESSION['test' . $i]);[/code]
Now PHP will parse the variable $i rather than treating it as text.

Also if you want to destroy a session you should use session_destroy();
Link to comment
Share on other sites

[!--quoteo(post=387119:date=Jun 23 2006, 06:26 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 23 2006, 06:26 AM) [snapback]387119[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Also if you want to destroy a session you should use session_destroy();
[/quote]
Thanks..that works.
Doesn't session_destroy kill all session data ..I just want to unset a particular variable.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.