Error handling in PHP

后端 未结 9 1703
长情又很酷
长情又很酷 2020-12-02 08:40

I\'m familiar with some of the basics, but what I would like to know more about is when and why error handling (including throwing exceptions) should be used in PHP, especia

9条回答
  •  不知归路
    2020-12-02 09:05

    Unhanded errors stop the script, that alone is a pretty good reason to handle them.

    Generally you can use a Try-Catch block to deal with errors

    try
    {
        // Code that may error
    }
    catch (Exception $e)
    {
        // Do other stuff if there's an error
    }
    

    If you want to stop the error or warning message appearing on the page then you can prefix the call with an @ sign like so.

     @mysql_query($query);
    

    With queries however it's generally a good idea to do something like this so you have a better idea of what's going on.

    @mysql_query($query)
        or die('Invalid query: ' . mysql_error() . '
    Line: ' . __LINE__ . '
    File: ' . __FILE__ . '

    ');

提交回复
热议问题