PHP: 'or' statement on instruction fail: how to throw a new exception?

后端 未结 7 1790
萌比男神i
萌比男神i 2020-12-06 04:30

Everyone here should know the \'or\' statemens, usually glued to an die() command:

$foo = bar() or die(\'Error: bar function return false.\');
7条回答
  •  误落风尘
    2020-12-06 05:20

    Here's a single-line solution without the extra function:

    if (!($foo = bar())) throw new Exception('We have a problem here');
    

提交回复
热议问题