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? Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/ 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 Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882941 Share on other sites More sharing options...
michalchojno Posted July 26, 2009 Author Share Posted July 26, 2009 Really? There must be a way. Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882942 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. Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882949 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. Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882951 Share on other sites More sharing options...
.josh Posted July 26, 2009 Share Posted July 26, 2009 $var = "variable"; $$var = "some text"; echo $variable; Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882954 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. Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882961 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? Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882963 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] Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882965 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? Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882967 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. Link to comment https://forums.phpfreaks.com/topic/167447-php-how-to-make-the-name-of-the-variable-be-the-same-as-another-variable/#findComment-882968 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.