6

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?

6
  • Why would you want to do this though? It's a bad idea. Fix the errors instead. Commented Aug 14, 2017 at 10:12
  • Because they are warnings not exactly errors Commented Aug 14, 2017 at 10:16
  • 3
    Possible duplicate of Remove warning messages in PHP Commented Aug 14, 2017 at 10:17
  • 2
    similar question stackoverflow.com/questions/1987579/… Commented Aug 14, 2017 at 10:17
  • 1
    @Bhautik Can you remove your comment and upvote mine? Same question, hehe! Commented Aug 14, 2017 at 10:18

2 Answers 2

14

Yes, but...

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

  1. 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..');

  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.