What is the best way to remove or turn off warning messages in PHP?

There will be times when you will see a warning message output to the browser after running your PHP script and you may want to turn off that warning message. Obviously, it’s a lot better to get to the root of the problem and fix that instead. But, if you know that you do not need to fix the root of the problem (for whatever reason), in order to remove a warning message in PHP all you have to do is use the error_reporting function in PHP. If you don’t care about how the function works, then just skip to the section that says “The code to turn off error messages in PHP” to see the code that you should use to turn off warning messages in PHP. Otherwise, keep reading.

Using the error_reporting function to turn off warnings in PHP

The error_reporting function in PHP basically allows you to set the kind of error reporting that you want. How does the error_reporting function work? Well, you simply pass in the type of errors to the error_reporting function that you want to have reported on the page – you need to pass in constants (which are text fields that translate to numbers) to the error_reporting function .

The E_PARSE constant tells PHP that compile time parse errors should be reported and displayed on the page as you can read about here. Since you definitely want to know about any compile time errors, you should pass this constant to the function. The E_ERROR constant tells PHP that the details of any fatal run-time errors should be reported and displayed – this is also something you definitely want, since you should always know what the cause of any fatal run-time errors is.

Now that you understand a bit more about how the error_reporting function works – here is the actual code to use:

The code to turn off error messages in PHP:

You have to place this line of code before the code that is causing the warning to be displayed. If you place this code after the offending code, then it will not work in suppressing the error message that gets displayed. Here is the line of code to use:

error_reporting(E_ERROR | E_PARSE);

The code above does not have the E_WARNING constant being passed in

Because the function above does not include the “E_WARNING” constant, the non-fatal run-time warnings will not be displayed on the page when a PHP script is run. And that is exactly what prevents the warning message from appearing on the page.

How do the constants work in the error_reporting function?

In the example above, E_PARSE and E_ERROR are both constants – which means that they are actually numbers represented by text, so E_PARSE really is just some text that represents the numeric value of 4, and E_ERROR is text representing the numeric value of 1. Read on to understand how those constants work.

How does the OR operator work with error_reporting function?

Note that the function above uses the “|” – the OR logical operator, which is applied against the constants that are passed into the error_reporting function. You will notice if you look at this page that those constants are all multiples of 2 – the reason for this is because when they are “OR’ed, the appropriate bits will be retained and that will tell the error_reporting function what errors need to be displayed.

Another way to hide or remove warning messages in PHP

Another option to remove warning messages in PHP is to use what is called the error control operator – which is basically just the at sign – the “@”. When the “@” sign is put in front of an expression, any error message that might be generated by that expression will be ignored.

The “@” error control prefix operator will even disable error reporting for critical run time errors. For this reason, you should only use this operator if you really know what you are doing. The “@” can only be used in front of expressions – so it can not be used in front of a function or class definition, a for loop, etc. But, it can be used in front of a call to a function. Here is what it would look like in that scenario:

@someFunctionCall( );

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

4 thoughts on “How to remove warnings in PHP?”