* 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.
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
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.
> 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.
Finding the argument order for a method takes ~3-5 seconds. Its irrelevant. array() vs [] is actually a bigger deal as its indicative of php's verbose syntax. But its not a killer.
People like JS because it got the fundamentals right. The bad parts are just details. PHP is bad because it got the fundamentals wrong. But people still harp on the details.
But the "details" grate daily. Poor reflection, no FP? Fine, you craft solutions that don't require them. But having to look up order of arguments for standard library functions is a pain in the ass.
PHP 5.3, which certainly does provide that, is more than two years old. (And it also introduced late static binding and a few other tweaks and benefits to make its OOP significantly better.)
What? Why is [] better? Because it saves you typing 5 extra characters?
A lot of little details make a big difference.
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).
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.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!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."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.)
This should help you.
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)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.