PHP equivalent for Ruby's or-equals (foo ||=bar)?

Viewed 10093

In PHP I often write lines like

isset($foo)? NULL : $foo = 'bar'

In ruby there is a brilliant shortcut for that, called or equals

foo ||= 'bar'

Does PHP have such an operator, shortcut or method call? I cannot find one, but I might have missed it.

8 Answers

I really like the ?: operator. Unfortunately, it is not yet implemented on my production environment. So, if I were to make this look ruby-ish, I would go for something like:

isset($foo) || $foo = 'bar';

Or, if you want it even shorter (slower, and may yield unexpected results):

@$foo || $foo = 'bar';
Related