In PHP, why does “or die()” work, but “or return” doesn't?

后端 未结 3 819
我在风中等你
我在风中等你 2021-02-18 18:26

In PHP, you can handle errors by calling or die to exit when you encounter certain errors, like this:

$handle = fopen($location, \"r\") or die(\"Cou         


        
3条回答
  •  离开以前
    2021-02-18 18:56

    I've also stumbled upon that once. All I could find was this:

    https://bugs.php.net/bug.php?id=40712

    Look at the comment down below:

    this is not a bug

    I've searched in the documentation and I think it's due to the fact that return 0 is a statement whereas die() is essentially an expression. You can't run $handle = return 0; but $handle = fun(); is valid code.

    Regarding error handling I would recommend custom codes or using custom handlers and triggers. The latter are described here for example.

提交回复
热议问题