If $ a = 5, $ b = 'a', what is the value of $$ b?

Explain this question to me:

Q: If the variable $ a is 5 and the variable $ b is equal to the character a, then what is the value of $$ b?

A: 5, reference to an existing variable.

+7
php
source share
6 answers

This is a variable variable . PHP will look for a variable with the name stored in the string $b . Therefore, if $b == 'a' , then $$b == $a .

This is very similar to pointers to C, except that they use variable name strings instead of memory addresses to point to each other. And you can play as many times as you want:

 $a = 5; foreach (range('b', 'z') as $L) { $$L = chr(ord($L) - 1); } echo $$$$$$$$$$$$$$$$$$$$$$$$$$z; 

Output:

 5 
+8
source share

-95 is the answer, as if u will be echo $b u will be output as " a " and if u echo $a u will exit, but as " 5 "

therefore, in this sense, when u $(echo $b) , which matches $(a) , so u will get it as " 5-100 ", which is equal to " -95 "

+1
source share
  $$b - 100 = $a - 100 // substituting $b=a = 5 - 100 = -95 
0
source share

Below is a good reference to PHP variables

http://php.net/manual/en/language.variables.variable.php

0
source share

I don't know if '?' mistaken in the statement of '$$ b? - 100 ', but I don’t think it will compile.

But:

 $a = 5 $b = 'a'; $c = $$b - 100; 

$ c will be equal to -95 because $$ b is a variable reference variable and provided that $ a = 5 allows $ a (5) - 100 or -95.

0
source share

the answer is -95

 $a - 100 
0
source share

All Articles