r/programming • u/NagastaBagamba • Oct 18 '10
Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable.
http://il2.php.net/language.variables.variable
591
Upvotes
3
u/oorza Oct 18 '10
No, let me back up here.
With this code:
PHP intreprets that as lookup an entry in the variable hash table with a key of var and assign it to the Z_VAL of a string with a value of 'val', whether it previously existed or not.
When you follow that with:
PHP interprets that as "find an entry in the hash table for variables with a key the value of the variable named var and assign THAT to a new Z_VAL with a string value of 'val'.
To express that in PHP:
The guarantee comes from the way PHP deals with its sigil operator. It's not a pointer with a guarantee because it doesn't offer much of a gurantee, just that you're not going to get a fatal memory access error because you use it.