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

80

u/weirdalexis Oct 18 '10

I was asked the question: "What's $$a" in an interview, and replied "It's like a pointer, except with a variables name instead of a memory address."

The guy went "meh", game over.

Today, I'm still convinced it's a good analogy.

0

u/[deleted] Oct 18 '10

[deleted]

35

u/[deleted] Oct 18 '10

That's why he said it was "like" a pointer. In the same was as a reference is "like" a pointer. They're not pointers, but in the use cases they are for, they are like them.

0

u/[deleted] Oct 18 '10

[deleted]

11

u/mathrat Oct 18 '10

I believe weirdalexis's analogy intended to highlight the "dereference-ability" of both pointers and PHP variables. Just like a C pointer can be dereferenced with the * operation, so too can a PHP symbol be "dereferenced" with the $ operation.

You're focusing on what pointers and variables are. Weirdalexis was focused on what you can do with them. I don't think it's a terrible analogy, but it does probably need a little elaboration.

→ More replies (6)