PHP 5 disable strict standards error
Asked Answered
F

7

174

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

Can anybody help ?

Flipper answered 8/8, 2009 at 14:6 Comment(2)
@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.Ashcroft
Also locate you php.ini file and copy it to /usr/local/php5/lib/Hurley
T
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.

Turin answered 8/8, 2009 at 14:20 Comment(6)
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.Kutzenco
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.Tadio
This doesn't work for me - had to use E_ALL & ~E_STRICT from Fake Code Monkey Rashid comment from answer belowZhdanov
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 ?Dolphin
@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.Ensoll
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?Eichhorn
S
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);

Stapleton answered 8/8, 2009 at 14:8 Comment(6)
+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.Ashcroft
Note: E_STRICT has only been part of E_ALL since php 5.4Brooder
@FakeCodeMonkeyRashid I wonder why that is? probably because then the evaulation order is important?Sideways
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);Prettify
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.Lipski
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)Lipski
R
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)

Rhetorical answered 11/7, 2012 at 9:46 Comment(5)
Thanks a lot - this did the trick (.htaccess solution) in PHP 5.4.7 - nothing else - even modifying the .ini - was doing the trick.Exterior
I used php_admin_value error_reporting for this to work (in the vhost config).Trapezohedron
@Seza, Correct Fixed it.Rhetorical
its not about the page, this method is preferred because most E_STRICT errors are compile-time and can not be overridden in runtimeKaiserslautern
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.Caird
D
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);
}
Dynamometer answered 12/8, 2011 at 19:30 Comment(0)
K
8

In php.ini set :

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
Kerianne answered 3/3, 2015 at 9:22 Comment(0)
U
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

Untinged answered 29/6, 2015 at 15:20 Comment(1)
This is really useful, I had debug mode on for one Wordpress install and didn't realise it did this. Thanks for the information!Canny
L
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);
Lipski answered 29/6, 2017 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.