Hacker News new | past | comments | ask | show | jobs | submit login

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.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: