psyickphuk Posted December 11, 2008 Share Posted December 11, 2008 Basically I have an array and I want go through it as follows: $field_list array("text1", "text2", "etc"); for each ($field_list as $field) { if (!isset($_SESSION[$field])) { $_SESSION[$field] = null; } } I tried with/without quotes but no joy. A bit of searching and I think I need to use variable variables but I'm not sure how to implement this? Any suggestions welcome Link to comment https://forums.phpfreaks.com/topic/136439-using-a-variable-as-a-session-variables-name/ Share on other sites More sharing options...
redarrow Posted December 11, 2008 Share Posted December 11, 2008 <?php session_start(); $a=array("a","b","c","d"); foreach($a as $b){ $_SESSION['result'][]=$b; } $result=implode(' ',$_SESSION['result']); echo $result; ?> <?php session_start(); $a=array("a","b","c","d"); foreach($a as $b){ $_SESSION['result'][]=$b; } if($_SESSION['result'][]=''){ $_SESSION['result']=NULL; } $result=implode(' ',$_SESSION['result']); echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/136439-using-a-variable-as-a-session-variables-name/#findComment-712113 Share on other sites More sharing options...
.josh Posted December 11, 2008 Share Posted December 11, 2008 Basically I have an array and I want go through it as follows: $field_list array("text1", "text2", "etc"); for each ($field_list as $field) { if (!isset($_SESSION[$field])) { $_SESSION[$field] = null; } } I tried with/without quotes but no joy. A bit of searching and I think I need to use variable variables but I'm not sure how to implement this? Any suggestions welcome You're using the names correctly. The problem is that you're checking to see if it's set, and if it's not, you're setting it to null...that's about as useful as setting a variable to 1 if it's 1.... also you forgot the = in the array assignment $field_list = array("text1", "text2", "etc"); But I assume that was a typo... Link to comment https://forums.phpfreaks.com/topic/136439-using-a-variable-as-a-session-variables-name/#findComment-712126 Share on other sites More sharing options...
psyickphuk Posted December 11, 2008 Author Share Posted December 11, 2008 Thanks redarrow I will try that later. You're using the names correctly. The problem is that you're checking to see if it's set, and if it's not, you're setting it to null...that's about as useful as setting a variable to 1 if it's 1.... Yes seems pointless to me too but if I don't null my variables then I get a variable undefined message when they're used. You could argue not to do this and to their existence before usage but this seems to work and I only have to do it once. Link to comment https://forums.phpfreaks.com/topic/136439-using-a-variable-as-a-session-variables-name/#findComment-712319 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.