Jump to content


Photo

Looping through an unset statement


  • Please log in to reply
6 replies to this topic

#1 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 23 June 2006 - 08:35 AM

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

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 23 June 2006 - 08:53 AM

It's working for me..

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 23 June 2006 - 09:44 AM

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

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

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

?>


#4 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 23 June 2006 - 09:50 AM

Hmm.. Let me try..

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

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]

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 11:26 AM

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:
unset($_SESSION['test' . $i]);
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();

#6 slanton

slanton
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationNew Zealand

Posted 23 June 2006 - 11:54 AM

[!--quoteo(post=387119:date=Jun 23 2006, 06:26 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 23 2006, 06:26 AM) View Post[/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.

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 02:43 PM

Yeah session_destory kills the whole session. You'll want to use unset to kill seperate session vars within a session.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users