phpdeprecateddeprecation-warningphp-8php-8.1

PHP Disable Deprecation Warnings for a Single Line


I currently have all errors enabled for my site:

error_reporting(E_ALL);

However, in PHP 8.1, some functions are now deprecated:

PHP Notice: Function date_sunset() is deprecated in index.php on line 14

Due to current requirements, I am unable to update the line to the non-deprecated alternative.

Is there a way to disable error reporting of deprecations just for this single line?


Solution

  • As a workaround, you can wrap your deprecated line like this:

    error_reporting(E_ALL & ~E_DEPRECATED);
    // call_deprecated_function_here()
    error_reporting(E_ALL);
    

    Or if you wish to simply toggle the deprecated flag, use this:

    error_reporting(error_reporting() ^ E_DEPRECATED); // toggle E_DEPRECATED (off)
    // call_deprecated_function_here()
    error_reporting(error_reporting() ^ E_DEPRECATED); // toggle E_DEPRECATED (back on)