Dragen Posted June 8, 2007 Share Posted June 8, 2007 Is it possible to set the name of a variable to the value of another variable. Lets say I have a variable: $myvariable = 'hello'; now I want to create another variable with it's own value, but I want the variable to be called whatever $myvariable is equal to. So in this instance it would look something like this: $hello = 'somevalue'; Is this possible? Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/ Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 $$myvariable = "somevalue"; echo $hello . " " . $myvariable; Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271029 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 I think you missunderstood me.. I don't want to echo two variables. If I have: $myvariable = 'goldfish'; Then my new variable should be called $goldfish or whatever $myvariable is equal to.. if $myvariable = 'ghdfghjkdf'; then my new variable should read that and call itself $ghdfghjkdf, and have a value which I've designated it. EDIT: here's the code I'm trying to put it in: <?php function get_variable_arrays($name){ $sql = "SELECT * FROM variables WHERE name = '" . $name . "'"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $VARIABLE[] = $row['value']; //I want $VARIABLE to be equal to whatever $name's value is. } }else{ echo 'ERROR: array not found!'; } }else{ echo mysql_error(); } } ?> Because I don't neccessarily know what $name is going to be, I just want to make the $VARIABLE called whatever $name is. If I echo $name it will display, for example helloworld Then $VARIABLE should be called $helloworld I hope I've explained myself.. Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271035 Share on other sites More sharing options...
chocopi Posted June 8, 2007 Share Posted June 8, 2007 couldnt you just have $variable = 'hello world'; $blah = $variable; I might have misunderstood though ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271067 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 no. That's giving $blah the valuse of 'hello world'. Instead of $blah, I want it to be called 'hello world', or whatever $variable has the value of. so my two variables would be: $variable = 'helloworld'; //somehow read the value of $variable and create a new variable with the name $helloworld = 'another value'; Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271074 Share on other sites More sharing options...
chocopi Posted June 8, 2007 Share Posted June 8, 2007 I dont know ??? Just out of intrest why do you need this Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271080 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 Just out of intrest why do you need this probably just because I'm an awkward b@stard... I'm just trying out different ideas really. I use this function several times and wanted to get a different array from it, each time. Instead of them all having the same name's and getting confused with one another, I wanted them to be different arrays. What I'll probably have to do is have a multi-array.. <?php function get_variable_arrays($name){ $sql = "SELECT * FROM variables WHERE name = '" . $name . "'"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $variable[$name][] = $row['value']; } }else{ echo 'ERROR: array not found!'; } }else{ echo mysql_error(); } } ?> so have the array $variable, which contains an array with the name of $name.. which contains the $row['value']. Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271089 Share on other sites More sharing options...
accident Posted June 8, 2007 Share Posted June 8, 2007 errr.... how would you be able to reference the variables name if you yourself wouldn't know it? and why the heck would you need this? An array or whatever would suit better Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271090 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 errr.... how would you be able to reference the variables name if you yourself wouldn't know it? well that's the problem I've got... and why the heck would you need this? as I said, probably just because I'm an awkward b@stard... It was just a passing idea... Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271093 Share on other sites More sharing options...
chocopi Posted June 8, 2007 Share Posted June 8, 2007 I dont think it is possible as you cant call a variable if you dont know it name, but its a nice idea and would be quite cool if it did work ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271095 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 Sorry to lead you astray. <?php $myvariable = "goldfish"; $$myvariable = " Why not buy one?"; echo $myvariable . " " . $goldfish; ?> Perhaps that clears it up for you. Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271096 Share on other sites More sharing options...
paul2463 Posted June 8, 2007 Share Posted June 8, 2007 here you go <?php $var1 = "hello"; $$var1 = "world"; echo $hello; //prints out "world" ?> Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271097 Share on other sites More sharing options...
chocopi Posted June 8, 2007 Share Posted June 8, 2007 however if you didnt know if was hello it wouldnt work, so what could this be used for ??? Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271099 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 haha! thanks I thought I was lost then.. and you had it all along frost. Thanks Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271101 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 haha! thanks I thought I was lost then.. and you had it all along frost. Thanks Yea, was just rushed when I posted, sorry about that, next time don't doubt my skillz =P Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271102 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 doubt your skills?? I wouldn't dream of it... Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271112 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 hmm.. I seem to be having trouble with it. here's my function: <?php function get_variable_arrays($varname){ $sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "'"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $$varname[$row['value']] = $row['value']; //here's my variable which takes the value of $varname } }else{ echo 'ERROR: array not found!'; } }else{ echo mysql_error(); } } ?> And here's how I'm calling it: <?php get_variable_arrays('allowtype'); print_r($allowtype); ?> That should work shouldn't it? I've set $varname as 'allowtype', which in turn creates the variable $allowtype ans sets it as an array with data on it.. That's I'd hoped anyway.. unfortunatly I get absolutly nothing. When I print the array it outputs nothing. Have I done something wrong? Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271157 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 It should work as long as it is in the same scope. Remember that in order for a variable to be read somewhere it has to be defined within that scope or defined as global. I am not sure if this would work but give it a try. <?php function get_variable_arrays($varname){ $sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "'"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ global $$varname; $$varname[$row['value']] = $row['value']; //here's my variable which takes the value of $varname } }else{ echo 'ERROR: array not found!'; } }else{ echo mysql_error(); } } ?> Unsure if that would work or not. Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271179 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 thanks for the help, but It didn't make any difference.. I've already tried using global.. in several different places with no luck. Link to comment https://forums.phpfreaks.com/topic/54808-set-variable-name-as-value-of-another-variable/#findComment-271182 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.