tags:

views:

96

answers:

5

I'd have expected ${$b} to be 'a' but it is 'b', why is this the case?

$a = "b";
$b = "a";

Sorry again I forgot to put ${$b} produces "b"

+2  A: 

It couldn't be.

$b = "a";
${$b} == $a;
$a = "b";
${$b} == "b"
SpawnCxy
+2  A: 

Only thing I can think of is that when you're getting its value you forgot the $. This is a pretty common typo for new php programmers (and even for experienced ones that program in other languages).

intuited
+3  A: 

If you use the ${$b} it is equal to $a. So that time it will print "b" only.

muruga
+1  A: 

It makes sense now with your addition

Sorry again I forgot to put ${$b} produces "b"

that works as intended: You are using $b (containing "a") as a variable name. So as the end result, you are querying $a.

Pekka
+5  A: 

The variable variable expression ${$b} takes the value of $b for the variable name. So ${$b} evaluates to ${"a"} that is equivalent to $a that then evaluates to "b".

Gumbo