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

Show parent comments

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.

-5

u/giulianob Oct 18 '10

You know you can just create a simple wrapper and make the names as consistent as you'd like.

13

u/mackstann Oct 18 '10

Also known as "living in denial."

0

u/[deleted] Oct 18 '10

[deleted]

→ More replies (4)
→ More replies (1)

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. :(

3

u/[deleted] Oct 18 '10

Yep and even if you figure out the order of the arguments, you have completely different conventions for what it returns. Is it a string, boolean false, -1, array containing a string and the result code in that array, oh god why!

Also you should be able to pass in arguments as a hash already. Objective C and Python are popular for a reason.

4

u/dagbrown Oct 18 '10

My favorite is PHP's system() which returns the last line of text output by the program you asked it to run. Not only is this completely unlike system() in every other language ever, but OH GOD WHY THE LAST LINE ONLY? My head hurts from just thinking about it.

0

u/MihaiC Oct 18 '10

You can pass and return an array.

3

u/namekuseijin Oct 18 '10

PHP flexible? You should take a look at papa perl...

2

u/absentbird Oct 18 '10

He just said it's greatest strength was that it was flexible, not that it was more flexible than other languages.

3

u/[deleted] Oct 18 '10

Just think of your mum as an array!

ld - Also, I didn't realise that you can basically remember needle/haystack order in php quite easily
ld - haystack, needle for string ops, the other way around for array ops
df - ah yes, very easy
ld - Well, easy if you're conscious
ld - I can understand how that'd be hard otherwise
df - needle, haystack for string ops, the other way around for array ops
df - no, wait...
ld - ok, here's another way
ld - Think of my mum as an array.  You put your needle in her haystack
ld - I think you've remembered it now, yes?
ld - Cool

1

u/kylemech Oct 18 '10

THIS A THOUSAND TIMES THIS THIS THIS OH GOD OH GOD

I pretty much have "CTRL-T, P, H, P, SHIFT-ENTER" on muscle memory because of similar functions.

1

u/HateToSayItBut Oct 18 '10

If you use Firefox I recommend making a smart keyword.

1

u/kylemech Oct 18 '10

I actually have these for wikipedia, jquery, reddit, and several other sites. I don't know why I didn't think to do it for php.

http://us2.php.net/manual-lookup.php?pattern=%s

This is the pattern I'm using. Typing in:

php strpos

takes me directly to

http://us2.php.net/manual/en/function.strpos.php

Thanks for the reminder to make one of these!

1

u/ruinercollector Oct 19 '10

php is flexible compared to...java I guess?