3

This works with simple variables. But it shows empty result with complex variables. AM I MISSING SOMETHING HERE? or is there anyother way around. Thanks.

#1. This works with simple variables.
$object = "fruit";
$fruit = "banana";

echo $$object;   // <------------ WORKS :outputs "banana".
echo "\n";
echo ${"fruit"}; // <------------ This outputs "banana".


#2. With complex structure it doesn't. am I missing something here?
echo "\n";
$result = array("node"=> (object)array("id"=>10, "home"=>"earth", ), "count"=>10, "and_so_on"=>true, );
#var_dump($result);

$path = "result['node']->id";
echo "\n";
echo $$path; // <---------- This outputs to blank. Should output "10".
JayKandari
  • 1,228
  • 4
  • 16
  • 33
  • 1
    I guess that it will search for some variable named `result['node']->id`, of course that variable does not exist (not wanting to say it's invalid), hence echoing empty. – King King Oct 18 '14 at 06:44
  • so, how would I achieve the result I need? – JayKandari Oct 18 '14 at 06:46
  • 1
    your requirement is very close to what `eval()` function can do. However it's still not what you want. I don't think you can achieve such a dynamic parsing (and also I'm not sure why you want to do something like that). If we can parse that string to evaluate the expression to some ***assignable*** result, it would make the `eval()` become redundant (or less powerful). – King King Oct 18 '14 at 07:32
  • +1 for eval(). See my answer below – Justin Kiang Oct 18 '14 at 07:48
  • There is an answer to a similar question. Check this http://stackoverflow.com/questions/2036547/variable-variables-pointing-to-arrays-or-nested-objects – geoandri Oct 18 '14 at 08:18
  • Please consider eval as a last option. See this [question on SO](http://stackoverflow.com/questions/951373/when-is-eval-evil-in-php) for why eval is (mostly) evil. – enricog Oct 18 '14 at 09:52

2 Answers2

0

Not exactly using variable variables, but if you want to use a variable as the var name, eval should work

$path = "result['node']->id"; eval("echo $".$path.";");

Justin Kiang
  • 1,270
  • 6
  • 13
0

From php.net's page on variable variables

A variable variable takes the value of a variable and treats that as the name of a variable.

The issue is that result['node']->id is not a variable. result is the variable. If you turn on error reporting for PHP notices you will see the following in your output:

PHP Notice: Undefined variable: result['node']->id ...

This can be solved as follows:

$path = "result";
echo "\n";
echo ${$path}['node']->id;

The curly braces around $path are required.

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

If not present the statement is equivalent to

${$path['node']->id}

which will result in the following output:

PHP Warning:  Illegal string offset 'node' in /var/www/html/variable.php on line 18
PHP Notice:  Undefined variable: r in /var/www/html/variable.php on line 18
PHP Notice:  Trying to get property of non-object in /var/www/html/variable.php on line 18
topher
  • 14,790
  • 7
  • 54
  • 70