I have some PHP code. When I run it, a warning message appears.
How can I remove/suppress/ignore these warning messages?
I have some PHP code. When I run it, a warning message appears.
How can I remove/suppress/ignore these warning messages?
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);
E_ALL ^ E_WARNING
, enabling all error reporting besides warnings, seems like a better choice of argument to error_reporting
.
– Mark Amery
Aug 25 '14 at 19:54
You can put an @ in front of your function call to suppress all error messages.
@yourFunctionHere();
dns_get_record
will throw warnings. Your code may compensate for the warning but it still throws them. Turning error reporting off works on the production server, but not on the devel server. If you are generating XML content, the warning will cause the browser not to render because the server is sending malformed XML caused by the warning. Sometimes you want that on devel, but not for something caused by a temporary DNS lookup failure you already compensate for.
– Alice Wonder
Feb 19 '15 at 21:39
To suppress warnings while leaving all other error reporting enabled:
error_reporting(E_ALL ^ E_WARNING);
If you don't want to show warnings as well as errors use
// Turn off all error reporting
error_reporting(0);
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);
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);
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.
ob_start()
and ob_end_clean()
instead. This way the stuff doesn't even get sent to the browser (which it does here).
– h2ooooooo
Dec 30 '12 at 20:07
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.
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
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.