0

Trying to parse command line options I've found the following code throws a notice:

$options = getopt('p:', array('path:'));
$options['p'] = 'some file path';
$path = isset($options['p']) ? $options['p'] : isset($options['path']) ? $options['path'] : null;

Notice:

Notice: Undefined index: path

Why aren't the assigneds evaluated left-to-right starting with the isset($options['p'])?

andig
  • 13,378
  • 13
  • 61
  • 98
  • I seem to recall from the [docs](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) that nested (aka stacking) ternary operators are a bit of a no-no. To quote: "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious: " – Darragh Enright Aug 27 '15 at 08:46
  • Do not use nested ternary operators without parenthesis (and even with it it's still ugly) - one day you ill break your leg with that. Meanwhile, all the info regarding the precedence is done [here](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) – Alma Do Aug 27 '15 at 08:47
  • Did you try this? `$path = isset($options['p']) ? $options['p'] : (isset($options['path']) ? $options['path'] : null);` – Arcanyx Aug 27 '15 at 11:34

0 Answers0