0

This may sound confusing:

$myVar = "Helloooo!";
$text = "myVar";

How could i call $myVar from just the fact $text is filled with the variable name, perhaps this ? (it doesnt work though)

echo $($text);
apaderno
  • 26,733
  • 16
  • 74
  • 87
tarnfeld
  • 24,994
  • 39
  • 109
  • 145
  • 1
    It may be best to avoid this. It's very fragile and error prone. You can achieve the same results using an array, like setting $a[$myVar] and using it instead of $$myVar. – Vinko Vrsalovic Dec 22 '09 at 17:52
  • duplicate of [Can I use a generated variable name in PHP?](http://stackoverflow.com/questions/130240/can-i-use-a-generated-variable-name-in-php), [how do i create a variable from another variable name?](http://stackoverflow.com/questions/664606/how-do-i-create-a-variable-from-another-variable-name) – outis Oct 01 '10 at 04:07

1 Answers1

5

PHP has a feature called "variable variables", that works exactly like you need it to.

You can use it almost like you posted, but without the brackets:

echo $$text;

The best notation is using curly braces however, as that removes ambiguities when dealing with arrays.

echo ${$text};
zombat
  • 90,260
  • 24
  • 155
  • 163