3

While browsing the code of an open source PHP library I spotted this line:

$path = $path ?: $this->guessPackagePath();

This is not the ternary operator like it usually apprears. Would anyone explain what is going on in it?

Desmond Hume
  • 8,037
  • 14
  • 65
  • 112

1 Answers1

9

It's a shortcut for

$path = $path ? $path : $this->guessPackagePath();

which comes from PHP 5.3

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • So what's going on in this unusual type of ternary op is that, if `$path` does not evaluate to `false` or an empty string, `$path` preserves its value, otherwise it's assigned to `$this->guessPackagePath()`, rite? – Desmond Hume Mar 26 '14 at 09:19
  • @DesmondHume Yes, that's right. – xdazz Mar 26 '14 at 09:22
  • Note that if `$path` is not yet set, that it will raise a PHP warning. – DanMan Jun 25 '14 at 15:03