5 ms·
PHP has its own beauty, such as arrays which can support numeric and string indexes at the same time. Rarely do I actually spit on my keyboard. Not only do mo
by aphyr 16y ago
PHP has its own beauty, such as arrays which can support numeric and string indexes at the same time.
Rarely do I actually spit on my keyboard.
Not only do most other languages do associative arrays properly, so, you know, numeric and string indices don't clobber each other, but they also don't explode when indexed with, say, anything but decimal literals. You seriously think this is beautiful behavior?
http://aphyr.com/journals/show/fun-with-php-arrays http://aphyr.com/journals/show/fun-with-php-arrays
- nickik 16y agoSomething hit my monitor too :) I have to tiwtter this.
- Androsynth 16y agoIf you write sloppy code, expect unexpected behavior. Your examples are as bad as any other junk out there. The advantage of php arrays (imho) is that you can treat them as associative arrays or not dependening on the situation. The lookup behavior is the same for both though, which is convenient. i don't know python or ruby enough to compare, but it is nicer than javascript where associative arrays (ie objects) and real arrays are entirely different types.
- edtechre 16y agoI write way more PHP than Python. But dicts are MUCH better in Python. In fact, dict keys can be any data type you choose, like a true hash table in other languages like Java (e.g. HashMap).
- Stormbringer 16y agoDon't blame the tool user for inconsistencies in the tool. A claw hammer that occasionally bites your hand off is a bad claw hammer. (Bad! No biscuit!)
- Androsynth 16y agoThe general consensus of this thread is that there are a lot of bad php developers out there that write bad code and this does serious discredit to the language. The example this poster linked to was bad code. Its not proof the language is bad, its proof the poster is no better than the truly bad programmers who use the language he is trying to make fun of.
- jessedhillon 16y agoIn PHP: $bool = (08 === 0); var_dump($bool); This prints bool(true) In other words, at the deepest level of identity this language can check, 08 is reported to be equal to 0. With a language like that, I think it's safe to blame the tool.
- aphyr 16y agoMy examples are meant to illustrate the broken behavior, not be real-world code. I structured it this way to make the broken-ness more apparent, but I guarantee you "good code" runs into these problems. But no other language I've used would work this way. hash[8] would be the same as hash[010] and hash[0x8], and distinct from hash['8']. In addition, if anybody told you that strings would be interpreted as integers, you'd expect that octal strings (which PHP obviously understands) would be interpreted as integers as well! Instead, only decimal formats are handled. This pernicious confusion of datatypes in the name of convenience is exactly what makes writing correct PHP code so infuriating. If you want to put the blame entirely on "bad PHP developers", I have some datetime and object-equality behaviors to discuss.
- aphyr 16y agoit is nicer than javascript where associative arrays (ie objects) and real arrays are entirely different types. Actually, while we're on the topic: these should be separate types, for exactly the reasons I outlined in that code snippet. Arrays and hashes have entirely different memory characteristics, access times, growth strategies, and indexing semantics! Trying to smush both models into the same construct is what leads to PHP's befucked implementation of "arrays"/"hashes", which succeed at being neither. As an example, compare Ruby (an abysmally slow language) to PHP on array iteration--a task in which PHP loses by a huge margin on both memory and time complexity.
- EGreg 16y agoI mean beauty in the sense of appreciating something. Just because your wife isn't a supermodel, doesn't mean you cannot find her beautiful and have a good marriage. Once you accept PHP for what it is, you find that there's a certain file-oriented style of coding that is perfectly suited to it, that embraces the unique type of PHP arrays, and that autoloads files when needed. If you want to see what I mean, just try the framework I built for an hour. It's more "php-like" than the rails clones out there.
- edtechre 16y agoNot to mention a couple of other things I hate about PHP arrays: 1) They don't extend stdClass so they aren't technically objects-- they're a different data type completely (WTF?) 2) Arrays are passed by value and not reference by default. Double WTF. So passing an array to a method call without the & reference operator is inefficient and prone to bugs as noobs don't expect the immutable behavior.
- edtechre 16y agoI could be wrong about the inefficiency, maybe arrays in PHP are implemented as "copy on write". But seeing how PHP is such a mess of a language, I somehow doubt it...
- pilif 16y agoThey ARE implemented as COW. It's discouraged to use explicit reference passing if you don't want to alter the array as PHP has to do a slight bit more work if you pass a reference. I think copy on write was added in the 5.1 timeframe (around 2005)
- edtechre 16y agoThanks for the info. I do think the default should have been pass by reference though, like with objects since PHP5.
- pilif 16y agoArrays (and everything else) should be objects. That would also solve the pass-by-reference issue and would clean up the global namespace that's littered with array_* functions.
- deleted 16y ago[deleted]
- wvenable 16y agoCopy-on-write has been around a lot longer than 5.1. I'd venture to say PHP 4 but someone will likely reply and say it was even earlier.