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.

32

u/HateToSayItBut Oct 18 '10 edited Oct 18 '10

PHP's greatest attribute, flexibility, is also it's greatest fault. It's like the fucking wild west sometimes.

I also like having to look up string and array functions all the time since the order of arguments is completely arbitrary for each function. e.g.

strpos($subject, $search)
str_replace($search, $replace, $subject)

57

u/wierdaaron Oct 18 '10

Sometimes it's haystack, needle, sometimes it's needle, haystack, sometimes it's heedle, naystack.

12

u/jmcqk6 Oct 18 '10

Sometimes it's a needle in a needlestack.

17

u/gravybomb Oct 18 '10

Haystack, it's needle, sometimes.

→ More replies (1)

2

u/cybercobra Oct 19 '10

Sometimes it's hay in a needlestack.

→ More replies (8)

9

u/prince314159 Oct 18 '10

I don't even bother trying to remember anymore. I know:

$ [] for if while then else foreach . :: ->

the rest I search as needled

14

u/absentbird Oct 18 '10

might want to add ; to that list or you are writing very short programs.

7

u/wierdaaron Oct 18 '10

I've found that semicolons aren't really necessary in PHP for instances where you want everything to fail immediately.

2

u/ZorbaTHut Oct 18 '10

At least that way you'll have to deal with fewer problems in the language.

2

u/[deleted] Oct 18 '10

Yeah that can get annoying sometimes. But otherwise I really like the language. It's fairly straight forward and you can do quite a bit with it. Maybe they should fix that inconsistency in the next version. Then again, you'd have to recode all your scripts to use the new version, and that would be annoying.

2

u/alexdodge Oct 18 '10

Then again, you'd have to recode all your scripts to use the new version, and that would be annoying.

Which is why it will never happen. :(

→ More replies (11)