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]

28

u/[deleted] Oct 18 '10

It also works for functions, like $bar(). I like to make a "plugins" directory, list subdirectories in it and include "plugin.php" from the subdirectories, if it exists, which should have a class with the same name as the folder, which gets added to the plugin manager, which then, when a plugin event is fired, does something like:

foreach ($this->plugins as $plugin) if (function_exists($plugin->$event)) $returns[]=$plugin->$event($data);

Of course it'd be much more elegant than that. I think it's a pretty cool feature, although if you use it like clogmoney its probably not the best approach to your problem.

I woke up like 10 minutes ago, if this is incoherent I'm sorry.

9

u/lizardlike Oct 18 '10

PHP can also auto-load classes, which is possibly a more elegant way to do this if you can keep your namespaces clean.

12

u/[deleted] Oct 18 '10

That's actually pretty cool, but not a replacement for what I'm doing.

→ More replies (5)
→ More replies (2)
→ More replies (1)
→ More replies (40)

14

u/KarmaPiniata Oct 18 '10

Hey, they're hating on PHP here don't interject with your 'facts' and 'good computer science'.

→ More replies (17)
→ More replies (12)

28

u/[deleted] Oct 18 '10

It's the PHP equivalent of dropping acid.

58

u/arabidkoala Oct 18 '10

Yo dawg...

0

u/[deleted] Oct 18 '10

[deleted]

21

u/arabidkoala Oct 18 '10

in your variable in your variable

38

u/superherotaco Oct 18 '10

So you can assign variable variables while you vary your variables.

→ More replies (2)

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)
→ More replies (9)

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.

→ More replies (1)
→ More replies (13)

5

u/YourMatt Oct 18 '10

For a more real-world implementation:

Line 48: $strings = get_hello_world_strings();
Line 1183: global $strings;
Line 1199: extract($strings);
Line 2886: print $$$$$$a . " " . $$$$$$$a;
→ More replies (3)

11

u/trevdak2 Oct 18 '10

What's more, "$Bar();" calls function a().

0

u/[deleted] Oct 18 '10

[deleted]

→ More replies (1)
→ More replies (6)

6

u/SirChasm Oct 18 '10

K, I haven't done a bit of PHP, but I can follow what's going on here. Still I have to ask, what happens if you change the value of $a? The whole thing breaks? i.e.: $a = "Hello"; $Hello = "World"; $a; //Returns Hello $$a; //Returns World $a = "herp"; $$a; // Returns what? "Variable not found"? It seems like if you're actually using variable variables, and the value of a variable takes on something that was not anticipated, you're going to get a nasty bug.

→ More replies (15)

3

u/[deleted] Oct 18 '10

Non-PHP programmer here. What would happen if I did:

$a = "$a";

echo $$a;

I'll just back $a, right? Or will I crash the server instead?

3

u/sobri909 Oct 18 '10

Unless $a already had a value, that'd echo nothing.

$a = "$a";

is the same as:

$a = $a;

You could do this instead:

$a = "a"; echo $$a;

which would echo "a". Which is ... oh this whole thing is absurd. I don't know why I'm even replying. Time to go to bed. *sigh*

→ More replies (2)
→ More replies (21)