janroald Posted February 7, 2007 Share Posted February 7, 2007 This one is kind of difficult to explain, but here it goes : Say $_SESSION[a] = 'true' Following url is of course invalid, but constructed to show my intention: index.php?name_of_variable=$_SESSION[a]&value_to_set=false; Is it possible to send a literal reference to a variable and then evaluate the reference so that i can update the variable without using its name? Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/ Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 You could try with variable variables but its not going to work with an array (sessions). My question really though is, why do you need this? Maybe if you explained it a little more we could find another solution. Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/#findComment-179028 Share on other sites More sharing options...
janroald Posted February 7, 2007 Author Share Posted February 7, 2007 I'm trying to update session variables through ajax requests. I want to send the "name" of the variable over url and the value to give it. usual solution is to do a standard : if($_GET[this]=='that') { $that = ... } else if ...etc But i'm just trying to research the possibility to create a reference to an actual variable based on a string that tells me the variable name. Create a reference from a variable variable maybe... Sound any less confusing at all? :-) Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/#findComment-179037 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 An example that may help. <?php session_start(); $_SESSION['that'] = 'bar'; echo $_SESSION['that'].'<br />'; if (isset($_GET['this'])) { // pass a url like ?this=that&val=foo if (isset($_GET['val'])) { $_SESSION[$_GET['this']] = $_GET['val']; } } echo $_SESSION['that']; ?> Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/#findComment-179041 Share on other sites More sharing options...
janroald Posted February 7, 2007 Author Share Posted February 7, 2007 No, not that easy :-) forget sessions, think just a simple variable $foo = 'bar'; // pass a url like ?this=foo&value=foobar How can i now, if at all possible, use $_GET[this] to create a reference to $foo and update it so that echo $foo; //foobar Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/#findComment-179050 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 Yeah... I don't think its possible as variable variable will not work with the superglobals. Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/#findComment-179051 Share on other sites More sharing options...
janroald Posted February 7, 2007 Author Share Posted February 7, 2007 Better example of what i'm trying to do. (of course, this dont work) // pass a url like ?this=foo&value=foobar $foo = 1; eval("\$bar =& \$$_GET[this];") $bar = 2; echo $foo; //2 Had a feeling this was a dead end, but had to try :-) Link to comment https://forums.phpfreaks.com/topic/37448-updating-a-variable-dynamically/#findComment-179054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.