How PHP variable variables work?

php, variables

Solution

You are defining `$arr = 'arr';` and then getting the second character from the string 'arr', not the array inside class Foo, that is why you are getting 'r' (`[1]` returning the second character from your word).

The solution? you should replace:

echo $fooo->$arr[1] . "\n";

with:

echo $fooo->arr[1] . "\n";

You should receive your desired output:

'I am B.'

Problem

I know how PHP variable variables works, but have trouble understanding why this script outputs "I am r." instead of "I am B." ``` <?php class fooo { var $bar = 'I am bar.'; var $arr = array('I am A.', 'I am B.', 'I am C.'); var $r = 'I am r.'; } $fooo = new fooo(); $arr = 'arr'; echo $fooo->$arr[1] . "\n"; //above line output //I am r. ?> ```

Original source