How to turn off error messages in PHP?

Member

by jasen , in category: PHP General , 2 years ago

How to turn off error messages in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by edmond_brakus , 2 years ago

@jasen 

To turn off error messages in PHP, you can use the error_reporting function. This function allows you to specify which types of errors should be reported and which should be suppressed.


For example, to turn off all error messages, you can use the following code:

1
error_reporting(0);


You can also use the @ operator to suppress error messages on a per-expression basis. For example:

1
@unlink($file);


This will suppress any errors that may occur when attempting to delete the file.


It's generally not a good idea to turn off error messages completely, as they can be useful for debugging and identifying problems in your code. However, there may be certain situations where you want to suppress error messages for some reason, such as when you are using PHP to output something that is intended to be displayed to end users. In these cases, the above methods can be useful.

Member

by aubrey , a year ago

@jasen 

I apologize, but I need to correct the previous response. The error_reporting function does not turn off error messages completely. It only sets the error reporting level.


To turn off error messages completely in PHP, you can modify the php.ini file or use the ini_set function.

  1. Modifying php.ini file:
  • Locate the php.ini file on your server. The location of this file varies depending on the server configuration.
  • Open the php.ini file in a text editor.
  • Find the following line: display_errors = On
  • Change it to: display_errors = Off
  • Save the changes and restart your web server for the changes to take effect.
  1. Using ini_set function:
  • Place the following code at the beginning of your PHP script:
1
ini_set('display_errors', '0');


This will turn off error messages for the specific PHP script.


It's important to note that turning off error messages completely is not recommended for production environments. It can make it harder to identify and debug issues in your code. It's best to handle errors appropriately and log them for debugging purposes.