Re:Short array syntax finally in PHP 5.4
by cheald on Monday July 25, 2011 @06:07PM
(#2804933)
In Python, if you're expecting a potentially missing key, you can just use dict.get('key') rather than dict['key']. The equivalent in PHP would be something like:
Being able to say "I expect a potentially nil value (and am okay with that)" via get() rather than array index is both semantically useful, and far more beautiful than the PHP equivalent.
[ Parent
| on HN ]
Sure, or you can catch a KeyError and do something else entirely. I'm aware of all this, just pointing out some differences to the person to whom I replied.
Python's dict.get is a bit like the getOrElse found in Scala. It's quite handy and can be seen as advanced conceptually.
I actually added some code like that to a recent post about PHP here: http://news.ycombinator.com/item?id=2771304