PHP: How to manage errors gracefully?

后端 未结 2 1270
南旧
南旧 2021-01-20 07:44

When failed to access something on the web (api, database), how would I stop executing the rest of the script and log the error in the log file? Well, and so that the visito

2条回答
  •  借酒劲吻你
    2021-01-20 08:14

    and so that the visitor wouldn't see the exact cause but rather they would see my custom message

    I would suggest you to go through this excellent article by David Walsh on custom error handling in PHP.

    You can use set_error_handler() function for that.

    how would I stop executing the rest of the script and log the error in the log file?

    There is a default function of PHP to log errors; error_log

    Example from PHP.net:

    
    

    .

    More Resources:

    This is also good article covering most of it related to error handling.

提交回复
热议问题