Jump to content

Archived

This topic is now archived and is closed to further replies.

slanton

Looping through an unset statement

Recommended Posts

I want to loop through an unset statement but can't seem to get the syntax right eg
[code]
for($i=0;$i<14;$i++){
unset($_SESSION['test$i']);
}
[/code]
doesn't work. Is this possible?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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]

Share this post


Link to post
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();

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Yeah session_destory kills the whole session. You'll want to use unset to kill seperate session vars within a session.

Share this post


Link to post
Share on other sites

×

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.