michalchojno Posted July 26, 2009 Share Posted July 26, 2009 How to make the name of the variable be a string of text, which you have in another variable? Example: $var = 'variable'; $variable = 'This is just some text'; Now I don't want to name it just $variable, but the name comes as a result of $var. How do I do that? I tried this, which didn't work: $var = 'variable'; ('$'.$var) = 'This is just some text'; // doesn't work Any tips? Quote Link to comment Share on other sites More sharing options...
MoMoMajor Posted July 26, 2009 Share Posted July 26, 2009 I don't think this can actually be done Quote Link to comment Share on other sites More sharing options...
michalchojno Posted July 26, 2009 Author Share Posted July 26, 2009 Really? There must be a way. Quote Link to comment Share on other sites More sharing options...
MoMoMajor Posted July 26, 2009 Share Posted July 26, 2009 What was your reason behind doing this? Maybe I an suggest a different way to do it. Quote Link to comment Share on other sites More sharing options...
michalchojno Posted July 26, 2009 Author Share Posted July 26, 2009 The only other way is "manual" if's. I think there must be a way to do that. In other languages this can be done. Quote Link to comment Share on other sites More sharing options...
.josh Posted July 26, 2009 Share Posted July 26, 2009 $var = "variable"; $$var = "some text"; echo $variable; Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 26, 2009 Share Posted July 26, 2009 in the real world, if your variable data (and variable name) are changing, you'll echo it this way: <?php $var = "variable"; $$var = "some text"; echo $$var; I don't know of a time where you will dynamically name a variable and then call a statically named variable. Quote Link to comment Share on other sites More sharing options...
michalchojno Posted July 26, 2009 Author Share Posted July 26, 2009 OK, your code works. Now why this code doesn't? $word = ('$'."$items5[$counter]".'5[$pid]'); $var5[$counter] = $$word; echo "$var5[$counter]"; The result is empty. EDIT: Perhaps the $ sign in variable $word? But I changed that and still no result. In other words that would go: $var = "variable[$counter]"; $$var = "some text"; echo $$var; Would that work? Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 26, 2009 Share Posted July 26, 2009 using this script: $counter = 1; $items5[1] = "test"; $word = ('$'."$items5[$counter]".'5[$pid]'); print $word; I got this echoed out: $test5[$pid] Quote Link to comment Share on other sites More sharing options...
michalchojno Posted July 26, 2009 Author Share Posted July 26, 2009 using this script: $counter = 1; $items5[1] = "test"; $word = ('$'."$items5[$counter]".'5[$pid]'); print $word; I got this echoed out: $test5[$pid] That's right. Me too. But now try: $whatever = $$word Will that work? Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 26, 2009 Share Posted July 26, 2009 you mean like this: <?php $counter = 1; $items5[1] = "test"; $word = ('$'."$items5[$counter]".'5[$pid]'); $$word = "hi"; echo $$word; yup. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.