In Python one can do:
foo = {}
assert foo.get('bar', 'baz') == 'baz'
In PHP one can go for a trinary operator as in:
$foo = array();
assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz');
I am looking for a golf version. Can I do it shorter/better in PHP?
A "slightly" hacky way to do it:
http://codepad.viper-7.com/flXHCH
Obviously this isn't really the nice way to do it. But it is handy in other situations. E.g. I often declare shortcuts to GET and POST variables like that:
PS: One could call this the "built-in
==$_=&
special comparison operator":PPS: Well, you could obviously just use
but that's even worse than the
==$_=&
operator. People don't like the error suppression operator much, you know.