Remove warning messages in PHP

PhpWarningsError Reporting

Php Problem Overview


I have some PHP code. When I run it, a warning message appears.

How can I remove/suppress/ignore these warning messages?

Php Solutions


Solution 1 - Php

You really should fix whatever's causing the warning, but you can control visibility of errors with error_reporting(). To skip warning messages, you could use something like:

error_reporting(E_ERROR | E_PARSE);

Solution 2 - Php

You can put an @ in front of your function call to suppress all error messages.

@yourFunctionHere();

Solution 3 - Php

To suppress warnings while leaving all other error reporting enabled:

error_reporting(E_ALL ^ E_WARNING);	

Solution 4 - Php

If you don't want to show warnings as well as errors use

// Turn off all error reporting
error_reporting(0);

Error Reporting - PHP Manual

Solution 5 - Php

If you want to suppress the warnings and some other error types (for example, notices) while displaying all other errors, you can do:

error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);

Solution 6 - Php

in Core Php to hide warning message set error_reporting(0) at top of common include file or individual file.

In Wordpress hide Warnings and Notices add following code in wp-config.php file

ini_set('log_errors','On');
ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

Solution 7 - Php

Not exactly answering the question, but I think this is a better compromise in some situations:

I had a warning message as a result of a printf() statement in a third-party library. I knew exactly what the cause was - a temporary work-around while the third-party fixed their code. I agree that warnings should not be suppressed, but I could not demonstrate my work to a client with the warning message popping up on screen. My solution:

printf('<div style="display:none">');
    ...Third-party stuff here...
printf('</div>');

Warning was still in page source as a reminder to me, but invisible to the client.

Solution 8 - Php

I do it as follows in my php.ini:

error_reporting = E_ALL & ~E_WARNING  & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

This logs only fatal errors and no warnings.

Solution 9 - Php

I think that better solution is configuration of .htaccess In that way you dont have to alter code of application. Here are directives for Apache2

php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

Solution 10 - Php

You could suppress the warning using error_reporting but the much better way is to fix your script in the first place.

If you don't know how, edit your question and show us the line in question and the warning that is displayed.

Solution 11 - Php

For ignoring all warnings use this sample, on the top of your code :

error_reporting(0);

Solution 12 - Php

NOTE: this is to suppress warnings, not fatal errors

You could make use of @ operator, this will hide all warnings except for the fatal errors, but mind you adding the @ operator will increase the time of execution for your script

eg @function_name();

the alternate way is to use the .ini configuration file and edit the error_reporting section, editing it as below will continue to show all errors but will suppress any warning in your script

error_reporting(E_ALL ^ E_WARNING);

but if you need to suppress warnings in a specific project and not your whole server then the below line of code should suffice

ini_set('error_reporting', E_ALL ^ E_WARNING);

Solution 13 - Php

There is already answer with Error Control Operator but it lacks of explanation. You can use @ operator with every expression and it hides errors (except of Fatal Errors).

@$test['test']; //PHP Notice:  Undefined variable: test

@(14/0); // PHP Warning:  Division by zero

//This is not working. You can't hide Fatal Errors this way.
@customFuntion(); // PHP Fatal error:  Uncaught Error: Call to undefined function customFuntion()

For debugging it's fast and perfect method. But you should never ever use it on production nor permanent include in your local version. It will give you a lot of unnecessary irritation.

You should consider instead:

1. Error reporting settings as mentioned in accepted answer.

error_reporting(E_ERROR | E_PARSE);

or from PHP INI settings

ini_set('display_errors','Off');

2. Catching exceptions

try {
    $var->method();
} catch (Error $e) {
    // Handle error
    echo $e->getMessage();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlirezaView Question on Stackoverflow
Solution 1 - PhpTatu UlmanenView Answer on Stackoverflow
Solution 2 - PhpPetPaulsenView Answer on Stackoverflow
Solution 3 - PhpKarthikView Answer on Stackoverflow
Solution 4 - Phpmohan.gadeView Answer on Stackoverflow
Solution 5 - PhpzstateView Answer on Stackoverflow
Solution 6 - PhpVijay LathiyaView Answer on Stackoverflow
Solution 7 - PhpDaveWalleyView Answer on Stackoverflow
Solution 8 - PhpnavidView Answer on Stackoverflow
Solution 9 - PhpSebastian PiskorskiView Answer on Stackoverflow
Solution 10 - PhpPekkaView Answer on Stackoverflow
Solution 11 - PhpAdaView Answer on Stackoverflow
Solution 12 - PhpAbdulbasitView Answer on Stackoverflow
Solution 13 - PhpJsowaView Answer on Stackoverflow