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".
6
  • 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. Commented Oct 18, 2014 at 6:44
  • so, how would I achieve the result I need? Commented Oct 18, 2014 at 6: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). Commented Oct 18, 2014 at 7:32
  • +1 for eval(). See my answer below Commented Oct 18, 2014 at 7:48
  • There is an answer to a similar question. Check this stackoverflow.com/questions/2036547/… Commented Oct 18, 2014 at 8:18

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.";");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.