slanton Posted June 23, 2006 Share Posted June 23, 2006 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? Link to comment https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/ Share on other sites More sharing options...
hackerkts Posted June 23, 2006 Share Posted June 23, 2006 It's working for me.. Link to comment https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/#findComment-48726 Share on other sites More sharing options...
slanton Posted June 23, 2006 Author Share Posted June 23, 2006 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 https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/#findComment-48734 Share on other sites More sharing options...
hackerkts Posted June 23, 2006 Share Posted June 23, 2006 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 https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/#findComment-48735 Share on other sites More sharing options...
wildteen88 Posted June 23, 2006 Share Posted June 23, 2006 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 https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/#findComment-48743 Share on other sites More sharing options...
slanton Posted June 23, 2006 Author Share Posted June 23, 2006 [!--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 https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/#findComment-48750 Share on other sites More sharing options...
wildteen88 Posted June 23, 2006 Share Posted June 23, 2006 Yeah session_destory kills the whole session. You'll want to use unset to kill seperate session vars within a session. Link to comment https://forums.phpfreaks.com/topic/12710-looping-through-an-unset-statement/#findComment-48803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.