174

I need to setup my PHP script at the top to disable error reporting for strict standards.

Can anybody help ?

Manny Calavera
  • 6,815
  • 20
  • 60
  • 85
  • 8
    @451F: I think the key words here are "strict standards". I don't know about previous versions but with PHP 5.4.0 it is recommended you set the error reporting to `E_ALL & ~E_DEPRECATED & ~E_STRICT` for production. Notice that they suggest you disable strict standards. – Fake Code Monkey Rashid Sep 01 '11 at 17:51
  • Also locate you php.ini file and copy it to /usr/local/php5/lib/ – yadhu Jul 18 '12 at 13:28

7 Answers7

187

Do you want to disable error reporting, or just prevent the user from seeing it? It’s usually a good idea to log errors, even on a production site.

# in your PHP code:
ini_set('display_errors', '0');     # don't show any errors...
error_reporting(E_ALL | E_STRICT);  # ...but do log them

They will be logged to your standard system log, or use the error_log directive to specify exactly where you want errors to go.

Nate
  • 18,752
  • 8
  • 48
  • 54
  • 11
    Just to explicitly state the obvious: Of course you can set these also in your `php.ini` file, e.g. if you cannot modify the PHP code. – sschuberth Sep 11 '13 at 14:04
  • 1
    Logging strict errors in production is also a bad practice, however. Since you'll fill your logs with notices that likely don't matter, causing one or both of the following issues: serverAdmin will miss/ignore errors and log directory will consume all server space at some point. – Lance Jan 18 '15 at 19:44
  • 4
    This doesn't work for me - had to use E_ALL & ~E_STRICT from Fake Code Monkey Rashid comment from answer below – besimple May 08 '15 at 09:05
  • 1
    how does this work alongside the following which I found in my php.ini log_errors = On Vs ini_set('display_errors', '0'); Is is last one set wins ? – landed Jun 05 '15 at 14:57
  • @nate when you post some code please tell us where we can paste it. I don't know where to put it: into `php.ini` or `.htaccess` or somewhere in my PHP code. – Даниил Пронин Dec 23 '15 at 12:27
  • Generally, is it safe to ignore "Strict Standards" errors by hiding them from the user or dumping them in logs? Or are they critical/ need fixing asap? – Hamman Samuel Mar 06 '16 at 16:04
90

For no errors.

error_reporting(0);

or for just not strict

error_reporting(E_ALL ^ E_STRICT);

and if you ever want to display all errors again, use

error_reporting(-1);

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
  • 56
    +1: I believe the `^` is only good for omitting one type of error. If you want to turn off additional types you should use the `E_ALL & ~E_DEPRECATED & ~E_STRICT` format. Or perhaps the `(E_ALL & ~(E_DEPRECATED | E_STRICT))` format. – Fake Code Monkey Rashid Sep 01 '11 at 17:57
  • 10
    Note: E_STRICT has only been part of E_ALL since php 5.4 – Fred Haslam Nov 18 '11 at 04:31
  • @FakeCodeMonkeyRashid I wonder why that is? probably because then the evaulation order is important? – codeling Dec 18 '13 at 22:16
  • Suppress reporting of STRICT errors in PHP < 5.4 `ini_set('error_reporting', E_ALL&~E_STRICT);` Suppress reporting of STRICT errors in PHP >= 5.4 `ini_set('error_reporting', E_ALL^E_STRICT);` – Mel_T Feb 24 '16 at 08:21
  • I want to point out that using `^` ("xor") rather than `& ~` ("and not") is a **bad idea**! `^` depends on the **assumption** that e.g. E_STRICT is part of E_ALL and *always* will be part of it. This is bad because E_ALL did change in the past (E_STRICT wasn't past of it, but is now since PHP 5.4). If the assumption fails one day, `^` will not only break, but actually do the **opposite** of what it's supposed to do: It will **enable** E_STRICT due to how XOR (`^`) works. `& ~` however will *always* disables E_STRICT, no matter the current value of E_ALL. Therefore `& ~` should be used. – Jay Jun 29 '17 at 21:39
  • Also "and if you ever want to display all errors again, use" and passing `-1` assumes that all errors were enabled before, which might not be the case and as a matter of fact rarely is. That's why you can first retrieve and store the preset value and properly restore it later (see my answer below) – Jay Jun 29 '17 at 22:08
31

All above solutions are correct. But, when we are talking about a normal PHP application, they have to included in every page, that it requires. A way to solve this, is through .htaccess at root folder. Just to hide the errors. [Put one of the followling lines in the file]

php_flag display_errors off

Or

php_value display_errors 0

Next, to set the error reporting

php_value error_reporting 30719

If you are wondering how the value 30719 came, E_ALL (32767), E_STRICT (2048) are actually constant that hold numeric value and (32767 - 2048 = 30719)

Starx
  • 77,474
  • 47
  • 185
  • 261
  • 2
    Thanks a lot - this did the trick (.htaccess solution) in PHP 5.4.7 - nothing else - even modifying the .ini - was doing the trick. –  Feb 07 '13 at 03:34
  • I used `php_admin_value error_reporting` for this to work (in the vhost config). – Lou Terrailloune Aug 28 '13 at 07:58
  • @Seza, Correct Fixed it. – Starx Aug 28 '13 at 16:31
  • 1
    its not about the page, this method is preferred because most E_STRICT errors are compile-time and can not be overridden in runtime – AbiusX Dec 26 '13 at 19:28
  • 2
    Hi just to make it little ease, for those who are using wamp, you can disable errors by clicking php > php settings >> display errors. If it is checked then uncheck it. – Hemang Rami Mar 27 '14 at 18:43
9

The default value of error_reporting flag is E_ALL & ~E_NOTICE if not set in php.ini. But in some installation (particularly installations targeting development environments) has E_ALL | E_STRICT set as value of this flag (this is the recommended value during development). In some cases, specially when you'll want to run some open source projects, that was developed prior to PHP 5.3 era and not yet updated with best practices defined by PHP 5.3, in your development environment, you'll probably run into getting some messages like you are getting. The best way to cope up on this situation, is to set only E_ALL as the value of error_reporting flag, either in php.ini or in code (probably in a front-controller like index.php in web-root as follows:

if(defined('E_STRICT')){
    error_reporting(E_ALL);
}
8

In php.ini set :

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
MSS
  • 3,520
  • 24
  • 29
4

WordPress

If you work in the wordpress environment, Wordpress sets the error level in file wp-includes/load.php in function wp_debug_mode(). So you have to change the level AFTER this function has been called ( in a file not checked into git so that's development only ), or either modify directly the error_reporting() call

Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • This is really useful, I had debug mode on for one Wordpress install and didn't realise it did this. Thanks for the information! – Tim Dec 16 '16 at 19:18
2

I didn't see an answer that's clean and suitable for production-ready software, so here it goes:

/*
 * Get current error_reporting value,
 * so that we don't lose preferences set in php.ini and .htaccess
 * and accidently reenable message types disabled in those.
 *
 * If you want to disable e.g. E_STRICT on a global level,
 * use php.ini (or .htaccess for folder-level)
 */
$old_error_reporting = error_reporting();

/*
 * Disable E_STRICT on top of current error_reporting.
 *
 * Note: do NOT use ^ for disabling error message types,
 * as ^ will re-ENABLE the message type if it happens to be disabled already!
 */
error_reporting($old_error_reporting & ~E_STRICT);


// code that should not emit E_STRICT messages goes here


/*
 * Optional, depending on if/what code comes after.
 * Restore old settings.
 */
error_reporting($old_error_reporting);
Jay
  • 3,640
  • 12
  • 17