Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)! Some consider this bad design, but I love this about PHP.


There 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.


I 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


It'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.


  > 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


This is one thing that really bugs me about the arguments against php. The language has fundamental flaws. Poor oop, no functional programming whatsoever, minimal reflection.

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.


Yes, PHP has fundamental flaws. But the only decent fix for that is to use a different language, and there may be many valid reasons why a developer is using PHP.

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.


No functional programming whatsoever?

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.)


Not to mention, some of the functions return an array, and some of the functions act of the array passed as argument. Unless you remember exactly, you'll constantly be checking the doc to verify the method does what you think it does.


> Arrays are created with array() as opposed to a more modern [].

What? Why is [] better? Because it saves you typing 5 extra characters?


Honestly? Yeah. The 5 characters adds up, typing it over and over, and stylistically, it looks nicer.

A lot of little details make a big difference.


WTF? Array functions as something GOOD about PHP? It doesn't have a slicing syntax for crying out loud!


Does 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).


Yes, 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.


> 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!


It doesn't matter. Rather, it matters to me. Why? Because I presumably actually have to use it.


What'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.


It 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.)



Are there any PHP array functions that you miss when you are using Python?


Imho, 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.


they 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.


In 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)


And 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: