3 ms·
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
by simonw 15y ago
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)