by ristretto · 15y ago · 0 comments
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.
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)