Reputation: 727
In order to disable PHP Warnings, you will need to set the following parameters in php.ini
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = On
or
display_errors = Off
The above method will disable warnings for the whole project. Is there a way to disable warnings for only a block of php code, i.e. disable warnings for certain functions or few lines of code?
Upvotes: 5
Views: 8666
Reputation: 14530
ideally you'd want to fix those errors (or at least handle them in a correct manner) as it just seems like bad design however what you're looking for is known as the error control operator (@
).
<?php
/* Intentional file error */
$my_file = @file('non_existent_file') or die("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.
Take note...
Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
Upvotes: 14
Reputation: 15629
First of all - supressing E_DEPRECATED
and E_STRICT
doesn't seems like a good solution - the next PHP Upgrade will break your code. Apply this should only be a temporal fix, till you fixed the error.
You have multiple options to supress errors/warnings.
Supress the error for a single statement, using @
-operator. Could be used like this: $content = @file_get_contents('some-url-which-is-maybe-not-available..');
Suppress the errors, till you want to revert it with error_reporting
. Doing this acts like setting other ini values.
An example would be
$oldlevel = error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
// some code ...
// revert it
error_reporting($oldlevel);
You should keep in mind, you can't remove compile time/parser errors/warnings for a few lines of a file. This could only be applied to the whole file, if you change the error level, before including it.
Upvotes: 2