Disable or Hide Error Messages in PHP

In this guide, we want to show you how to Disable or Hide Warning and Error Messages in PHP. PHP is a server-side programming language that is used for web development. One of the important processes in PHP is handling and debugging errors. But seeing the errors directly on the webpage is not looking good and professional. So you can follow the steps below to hide the error messages in PHP.

What Are PHP Error Reporting Levels? 

For the start, we want to understand the error types that can happen in PHP. Here we provide a list to understand them:

PHP Error TypeDescription
E_ERRORA fatal run-time error that can’t be recovered from. The script execution is halted.
E_WARNINGRun-time warning that doesn’t stop script execution.
E_PARSECompile-time parse errors.
E_NOTICERun-time notice, indicating something that might be an error.
E_ALLAll errors and warnings (including E_STRICT and E_DEPRECATED).

Steps To Disable or Hide Warning and Error Messages in PHP

Now that you understand the PHP error types, you can follow the steps below to control the error messages in PHP.

Step 1 – Control What Kind of Error Messages PHP Should Report

At this point, you can control What Kind of Error Messages PHP Should Report. To do this, you need to use the `error_reporting()` function. For example, you can use the following PHP code to report all errors except notices:

error_reporting(E_ALL & ~E_NOTICE);

Hide All PHP Error Reporting

Also, you can hide all the error reporting by setting this value to 0:

error_reporting(0);

This will hide the errors and prevent them to displayed.

Step 2 – Turn Off Display Error Messages in PHP

At this point, you can use the`ini_set()` function to control whether or not errors should be displayed to the user. For example, if don’t want users to see any error messages, you can use this function with the display_errors option:

ini_set('display_errors', 0);

This will turn off the errors that are printed on the screen.

Step 3 – PHP logging Error Messages

If you want to hide errors from the users but still want to know when they happen, you can use the ‘log_errors’ option. You can set this option to On and set a path file for error logs:


ini_set('log_errors', 'On');
ini_set('error_log', '/path/to/error.log');

Step 4 – Use PHP Try/Catch Blocks To Handle Code Error

At this point, you can use the try-and-catch blocks to handle the errors in PHP. With this option, if the ‘try’ block fails, the code execution will move to the ‘catch’ block.

try {
    // Code that might fail
} catch (Exception $e) {
    // Code to handle the error
    // $e is the exception object, which contains information about the error
}

Also, PHP has several Error Handling Functions that you can use:

  1. set_error_handler(): Sets a user function to handle errors.
  2. trigger_error(): Creates a user-level error message.
  3. error_get_last(): Gets information about the last error that occurred.
  4. error_log(): Sends an error message to the web server’s error log or to a file.

Conclusion

At this point, you have learned to Disable or Hide Warning and Error Messages in PHP. As you saw, PHP provides several handling error functions that you can use to handle your code errors.

Hope you enjoy it. You may like these articles on the Orcacore website:

Fix phpMyAdmin SSL Error Connection

Change PHP settings on DirectAdmin From Admin panel

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!