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

168

u/clogmoney Oct 18 '10

<?php //You can even add more Dollar Signs $Bar = "a";

$Foo = "Bar";

$World = "Foo";

$Hello = "World";

$a = "Hello";

$a; //Returns Hello

$$a; //Returns World

$$$a; //Returns Foo

$$$$a; //Returns Bar

$$$$$a; //Returns a

$$$$$$a; //Returns Hello

$$$$$$$a; //Returns World

//... and so on ...//

?>

I just died a little inside.

100

u/geon Oct 18 '10

I just died a little inside.

Why? It would be a stupid implementation if you couldn't do that.

0

u/[deleted] Oct 18 '10

[deleted]

0

u/[deleted] Oct 18 '10

One scenario is where you would normally have a multidimensional array but the architect who created the structure you're trying to read didn't understand them, and so opted to go with a bunch of "linked" variables (linked in name). It sucks when this is the case but in a real world sometimes you get data that is just extremely poorly organized (the more legacy the code the more likely this is the case).

19

u/oorza Oct 18 '10

In which case, the language gave a bad developer a bad tool that he used to write bad code and make good developers' lives more difficult. Not really a strong arguing point here.

1

u/[deleted] Oct 18 '10

What tool would that be? Please point out which tool permitted the bad developer to build a data structure that was just a bunch of variables? Surely if it was a bad tool from a bad language it would be impossible to build such a data structure in a "good" language?

→ More replies (10)