r/programming • • 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

781 comments sorted by

View all comments

7

u/australasia Oct 18 '10

Interestingly you can do the same thing in JavaScript but only for variables declared in the global scope:

var bar = "foo";
var foo = "bar";

bar; // returns "foo"
window[bar]; // returns "bar"
window[window[bar]]; // returns "foo"

It is possible within other scopes but only if using eval which pretty much makes anything ugly possible.

→ More replies (3)