174

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

Can anybody help ?

asked Aug 8, 2009 at 14:06
2
  • 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. Commented Sep 1, 2011 at 17:51
  • Also locate you php.ini file and copy it to /usr/local/php5/lib/ Commented Jul 18, 2012 at 13:28

7 Answers 7

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.

answered Aug 8, 2009 at 14:20
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
This doesn't work for me - had to use E_ALL & ~E_STRICT from Fake Code Monkey Rashid comment from answer below
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 ?
@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.
|
91

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);

answered Aug 8, 2009 at 14:08

6 Comments

+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.
Note: E_STRICT has only been part of E_ALL since php 5.4
@FakeCodeMonkeyRashid I wonder why that is? probably because then the evaulation order is important?
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);
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.
|
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)

answered Jul 11, 2012 at 9:46

5 Comments

Thanks a lot - this did the trick (.htaccess solution) in PHP 5.4.7 - nothing else - even modifying the .ini - was doing the trick.
I used php_admin_value error_reporting for this to work (in the vhost config).
@Seza, Correct Fixed it.
its not about the page, this method is preferred because most E_STRICT errors are compile-time and can not be overridden in runtime
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.
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);
}
answered Aug 12, 2011 at 19:30

Comments

8

In php.ini set :

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
answered Mar 3, 2015 at 9:22

Comments

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
11.6k5 gold badges46 silver badges49 bronze badges
answered Jun 29, 2015 at 15:20

1 Comment

This is really useful, I had debug mode on for one Wordpress install and didn't realise it did this. Thanks for the information!
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);
answered Jun 29, 2017 at 21:51

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.