295

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

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

Banee Ishaque K
  • 531
  • 8
  • 21
Alireza
  • 5,444
  • 9
  • 38
  • 50

11 Answers11

463

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);
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
  • 80
    `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
  • 1
    generally I agree, in my case generating the warning message was intended behaviour because it was part of my unit tests. – pgee70 Oct 03 '17 at 10:15
  • 1
    What does the "^" operator do? – N S Jan 20 '21 at 20:23
  • 3
    @NS The ^ is the xor (bit flipping) operator... In that example, as `E_ALL` have all errors bit set ("on") so when you do `^ E_WARNING` after it, you are flipping the bit of `E_WARNING`, so its "off"... – caiovisk Nov 09 '21 at 02:58
148

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

@yourFunctionHere();
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
PetPaulsen
  • 3,442
  • 2
  • 22
  • 33
  • 7
    This just hides the error, it's still happening. Errors make PHP slow down so it's best to fix the error if at all possible. Using @ even slows down the code when there is not an error. http://vega.rd.no/articles/php-performance-error-suppression – dprevite Jan 01 '10 at 02:31
  • 65
    Sometimes (unfortunately) you really don't have a choice. For example, the PHP function parse_url() generates Warnings for "severely malformed" URLs - which is arguably a bug since the function returns false in this case. So you must either tolerate these PHP warnings in your program output (may be unacceptable for parser/validator applications), suppress the Warnings somehow, or work around the broken PHP behavior by writing your own parser/validator for URLs. In this case, I choose the @. – Peter Dec 11 '12 at 20:14
  • 1
    IMPORTANT: Use this method iff (1) you can't fix the problem that generates the warning nor (2) hide your warnings from end users via php error_reporting... Hiding warnings from your developers is NOT a solution. – Joshua Kissoon Jun 26 '14 at 11:42
  • 5
    Let me explain why this is critical. Some functions like `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
  • 2
    Let me explain why this is useful, when instead of all warning messages, it is you who wants to handle the warning as an error and generate your own error message – Daniel N. Jun 07 '15 at 02:39
  • This is good for surprising deprecated function warnings until I can get to update the code. The '@' is an effective @todo marker. And it means my XML/JSON is well formed as in Alice's comment. – G O'Rilla Jul 18 '15 at 16:20
  • Another useful case for it is rmdir when you only want to delete a directory if it is not empty. Since rmdir() will not delete non-empty dirs, I can either scan it first and not call rmdir() if it is non-empty (which takes extra time), or just call rmdir() directly and fail on ENOENT, saving a few lines of code and disk access. – George Y. Feb 15 '16 at 22:11
  • It's handy for debug functions too, where an exception handler will write various variables to a log file without bothering to check if they're defined. – bp. Jun 30 '17 at 03:25
  • This should be handled via setting a temporary error handler then restoring the original error handler after the function has ended. – Troy Knapp Sep 08 '17 at 13:12
  • What about filesize when you don't care if the file does not exist / can't be read and the return value of false is fine. An @ symbol at the front in that scenario works fine. – Liam Mitchell Aug 29 '18 at 21:14
  • Never use the @ symbol for any reason. Google it. You'll end up hating yourself one day if you do. – John Hunt Oct 23 '18 at 08:26
  • PHP, pre 7.1 / mysqli, used to silently ignore parts of MySQL queries that were "invalid" because of unset variables (either in PHP or in the database). I was cool with that as I don't want to write code to pre-check every possibility of my more complex queries before I make the query. As my queries have been good for the past 9 years I have no desire to change them; the @ prefix is perfect for me. Thanks :) – Skyfish Jul 19 '23 at 12:22
64

To suppress warnings while leaving all other error reporting enabled:

error_reporting(E_ALL ^ E_WARNING); 
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Karthik
  • 1,428
  • 3
  • 17
  • 29
36

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

MD XF
  • 7,860
  • 7
  • 40
  • 71
mohan.gade
  • 1,095
  • 1
  • 9
  • 15
30

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);
zstate
  • 1,995
  • 1
  • 18
  • 20
22

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);
Vijay Lathiya
  • 1,087
  • 11
  • 14
13

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.

honk
  • 9,137
  • 11
  • 75
  • 83
navid
  • 1,022
  • 9
  • 20
11

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.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
DaveWalley
  • 817
  • 10
  • 22
  • 5
    Personally, I'd use [`ob_start()`](http://php.net/manual/en/function.ob-start.php) and [`ob_end_clean()`](http://php.net/manual/en/function.ob-end-clean.php) instead. This way the stuff doesn't even get sent to the browser (which it does here). – h2ooooooo Dec 30 '12 at 20:07
  • 6
    That's why I included "some situations", "warnings should not be suppressed" and "reminder to me". – DaveWalley Dec 30 '12 at 20:10
  • 2
    Before down-voting me more, please read my reply. Note "third-party", "compromise" and the comment above. I added this answer in case someone else is in the same situation I was in, and in that specific situation, this saved me whereas all other answers would not have been as good. Next time I will think twice about being helpful. – DaveWalley Sep 17 '14 at 14:49
  • Thank you for not removing this brilliant idea. On my system, I save all the errors and warnings, sent back from the clients and when an admin logs in, they are immediately alerted to any such activity so that they can advise the maintenance crew. The site is for educational games so it's not a biggie if anything goes wrong for an hour or so. – cneeds Nov 05 '17 at 01:44
8

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
Sebastian Piskorski
  • 4,026
  • 3
  • 23
  • 29
6

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

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();
}
Jsowa
  • 9,104
  • 5
  • 56
  • 60