开发者

Why is $b "b" in this logic when echoing out ${$b}

I'd have expected ${$b} to be 'a' but i开发者_JS百科t is 'b', why is this the case?

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

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


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


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


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


It couldn't be.

$b = "a";
${$b} == $a;
$a = "b";
${$b} == "b"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜