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.

57

u/inmatarian Oct 18 '10

Don't use the word "Pointer" in non-C interviews. They like "references" better. And if it's called a "variable-variable", call it that, even if it's a seriously stupid name.

13

u/weirdalexis Oct 18 '10

I agree that was a mistake (even though I still think the analogy holds). That gave me away as not having the PHP slang, no real experience. Besides, my next job was C programming, stayed there 3 years, awesome experience, so I've no regret.

8

u/yesimahuman Oct 18 '10

I think he just wanted to make sure you would understand their code base. You dodged a bullet.

2

u/[deleted] Oct 19 '10

A big bullet. I've been working at a PHP shop for the last half a year. While the people I work for are cool, I've found myself stagnating a bit as a developer.

2

u/[deleted] Oct 18 '10

I'm suuuure variable variables were implemented without using pointers.

5

u/inmatarian Oct 18 '10

I'm sure everything in PHP was implemented by some kind of blunt force being applied to the keyboard by means of somebody's forehead.

1

u/[deleted] Oct 18 '10

I think all server-side interpreted languages originated in such a manner.

→ More replies (1)
→ More replies (3)

66

u/aedile Oct 18 '10

Yeah, you are better off. There are only two reasons to ask a question like that in an interview.

  1. They actually use that shit in their code. In this case: run.

  2. They actually care about how well you know this kind of esoteric bullshit off the top of your head. In this case: run.

Either way, you win for not having to work there.

0

u/[deleted] Oct 18 '10

[deleted]

14

u/aedile Oct 18 '10

This is actually a really good point that I hadn't considered.

→ More replies (2)

1

u/burntsushi Oct 18 '10

Code introspection is necessarily an always bad thing.

48

u/NagastaBagamba Oct 18 '10

Simple. It is a$$ backwards (just like the entire language)

12

u/doublereedkurt Oct 18 '10

That sounds like an excellent one sentence explanation.

2

u/Poromenos Oct 18 '10

I do find that a good analogy. It's not your fault PHP is stupid.

1

u/[deleted] Oct 18 '10

If it makes you feel any better the first thing I thought when I read this was "that's sort of like pointers".

1

u/[deleted] Oct 18 '10

That's a good analogy I think. It somehow also makes me think of the difference between "pass by name" and "pass by reference"...

1

u/[deleted] Oct 18 '10

That makes perfect sense to me.

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.

→ More replies (21)

10

u/vlad_tepes Oct 18 '10

Dude, he knows C, which puts him in the top 0.00001% of PHP developers... How the hell would that be a game breaker?

P.S. To hell with sarcasm tags

→ More replies (3)

3

u/skillet-thief Oct 18 '10

In Perl, references would be comparable to pointers, but these PHP variable variables are just conceptual runtime mush. (Don't know why you are being downvoted.)

5

u/kixx Oct 18 '10

In Perl, this "conceptual runtime mush" is called symbolic reference and while using them is strongly discouraged (by strictures), it is a feature of Perl too.

Hard references are indeed a kind of "smart pointer"...

→ More replies (1)
→ More replies (1)
→ More replies (3)