开发者

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

开发者 https://www.devze.com 2022-12-24 03:12 出处:网络
I\'d have expected ${$b} to be \'a\' but i开发者_JS百科t is \'b\', why is this the case? $a = \"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

精彩评论

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