13 ms·
PHP is what I've started with and after many projects in Python and Java I still come back to it whenever I need to get stuff done. I mean look at the array fun
by saibotd 15y ago
PHP is what I've started with and after many projects in Python and Java I still come back to it whenever I need to get stuff done. I mean look at the array functions (http://www.php.net/manual/en/book.array.php http://www.php.net/manual/en/book.array.php)! Some consider this bad design, but I love this about PHP.
- rbanffy 15y agohttp://docs.python.org/tutorial/datastructures.html#list-comprehensions http://docs.python.org/tutorial/datastructures.html#list-com... This should help you.
- TylerE 15y agoWTF? Array functions as something GOOD about PHP? It doesn't have a slicing syntax for crying out loud!
- Udo 15y agoDoes it really matter to you whether array slicing is implemented as syntactical sugar as opposed to the array_slice() function? If so, why? In my view the only problem with PHP array functions is the fact that the naming scheme is an absolute disaster (as it is all over PHP, for historical reasons).
- TylerE 15y agoYes, it DOES. Syntax matters. Small improvements in a few places add up. For example, let's say you've got a string like 4|1|45|343|22. You want to return a string with the last three numbers, but seperated by a comma and a space. In PHP: $arr = explode('|',$str); $last_three = array_slice($arr, -3); return implode(", ",$last_three); In Python: return ', '.join(str.split('|')[-3:]) Small conciseness improvements snowball as the codebase gets larger. In fact, being unable to chain functions in PHP is actually my biggest beef with the language. If you could do return implode(', ',array_slice(explode('|',$str),-3)); that'd be a lot less annoying. Still not as good as the python, but a lot closer.
- Udo 15y ago> In fact, being unable to chain functions in PHP is actually my biggest beef with the language. That's just a lie or at least a horrible misconception, depending on your intentions. Furthermore, the example line you used to illustrate this entirely made-up inability of PHP does in fact work. You can do return implode(', ',array_slice(explode('|',$str),-3)); it's valid code and it works just as expected!
- burgerbrain 15y agoIt doesn't matter. Rather, it matters to me. Why? Because I presumably actually have to use it.
- Udo 15y agoWhat's that supposed to mean? That PHP devs don't actually have to use array slicing? That languages without a dedicated syntax for array slicing are unusable? Or that you prefer a syntactical shortcut and everyone who disagrees is an idiot? I mean I'm with you on the fact that it would be nice to have this in PHP. I just don't get how you can argue this point to show that it's supposedly a bad language. The difference between array_slice($array, -3) and $array[-3:] isn't that important to a lot of people. Interestingly, this criticism never comes up when, say, C# or Java are discussed. Only PHP is treated this way. As I said: sure it would be nice to have this feature, but I wouldn't imply that any language that doesn't have it is unusable. Ruby and Python are fortunate in this regard, but this feature alone wouldn't sway my choice either way. In the end, the number of array slicing operations per line of code isn't usually that big.
- burgerbrain 15y agoIt means that although it does not truly effect the languages effectiveness, it does negatively effect those who use it. Syntactical sugar exists because people have to use these languages. There is a reason so many languages of the same niche as PHP include array slicing. "Or that you prefer a syntactical shortcut and everyone who disagrees is an idiot?" No, not everyone who disagrees with me is an idiot. Just those people who read the sentence "Rather, it matters to me." and somehow think I mean "everyone who disagrees is an idiot", apparently missing the EMPHASIS ON "ME". (capped for your benefit.)
- deleted 15y ago[deleted]
- deleted 15y ago[deleted]
- generalk 15y agoThere are some good reasons to like PHP but the array functions in the standard library are not one of them. * Arrays are created with array() as opposed to a more modern []. * Arrays are not objects, so you have a ton of functions in the global namespace starting with array -- array_join() as opposed to $array->join(). * The functions are not consistent. Half of them don't have "array_" as a prefix. The order of arguments isn't consistent with other parts of the standard library. I'm really curious about why you consider this a strong point of PHP.
- sequoia 15y agoI hear what you're saying about arrays not being objects and there's really no excuse for the conflicting ($array, $argument) & ($argument, $array) signatures of many functions, but regarding "array() as opposed to a more modern []:" whenever I read this I can't help but think "Who fucking cares??" When people want to pick nits about this or significant whitespace it makes me think that there must very little variance between the languages if THIS is the issue that deserves scrutiny. Am I missing something about array() vs [] ? if you really hate typing array(), http://codepad.org/ASREdjTK http://codepad.org/ASREdjTK
- ianterrell 15y agoIt's not nitpicking to want consistency of convention. When I work with an unfamiliar class in Ruby, or come back to something familiar (say, Array) after a long time away, I'll often not "know" the exact method signatures for what I want, but I'll just guess something reasonable based on my knowledge of the language and idioms. And, lo and behold, most often it works! Having to "remember" (does this method use array_?) or "translate" is a mental tax; there's cycles wasted that could keep your mind in the business logic.
- generalk 15y ago> When people want to pick nits about this or significant whitespace > it makes me think that there must very little variance between > the languages if THIS is the issue that deserves scrutiny. For the record: this is not the issue that deserves scrutiny. The other issues are more significant. However this "short array syntax" is representative of PHP's sluggish internal development process. [] is very nearly the de-facto solution for creating arrays, and yet the PHP core devs have, for two years, rejected[1] patches that implement it on ridiculous grounds. [1]: https://wiki.php.net/rfc/shortsyntaxforarrays https://wiki.php.net/rfc/shortsyntaxforarrays
- simonw 15y agoAre there any PHP array functions that you miss when you are using Python?
- ristretto 15y agoImho, php arrays are not better but more convenient for the job: they are silent, not throwing exceptions when keys dont exist etc, they are all hashtables so you can use them as both arrays and lists, "foreach" syntax is better, nonexistent values convert to "" or 0 appropriately, you can do $a[1][2][3][4][5]++ without defining $a etc. Not better, just more convenient when you're in a hurry.
- simonw 15y agoIn Python: foo = mydict[key] # Throws exception if key does not exist foo = mydict.get(key) # Returns None if key does not exist PHP foreach compared to Python foreach: # PHP foreach ($arr as $value) { echo "Value: $value<br />\n"; } # Python for value in arr: print "Value: %s<br>" % value # PHP foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br />\n"; } # Python for key, value in arr.items(): print "Key: %s Value: %s<br>" % (key, value)
- malero 15y agoAnd then in a couple of months when you have to come back and figure out where that weird bug is hiding, you probably will wish that you spent a little more time writing your code properly. Just because PHP lets you write horrible code doesn't mean you should.
- rmc 15y agothey are silent, not throwing exceptions when keys dont exist etc I think this is a bad feature and causes bugs. In Python there's a fail fast feature, where it'll throw an error if you go past the end of an array. It's places like this were bugs creep in. In python, you find out that there's a problem with this array, in PHP you have to hunt around and only later look at the array X lines above where the bug manifests. they are all hashtables so you can use them as both arrays and lists Python dicts (i.e. hashtables) are better than PHP arrays. PHP arrays only have number or string keys, so you can't use arrays as a key in a PHP array. You can in python.