PHP's error_reporting() function allows a script to check what the current error reporting level is and/or modify it. I wouldn't normally recommend changing the error reporting level programatically, but there may be times when it's needed. The nice thing is it's easily possible to get the current level, change it, and then set it back to what it was previously.
Getting the current reporting level
To get the curent reporting level simply call the error_reporting() function without passing any parameters, like so:
This will return an integer value. You can see for example if E_NOTICE is set in the error reporting level like so:
Changing the error reporting level
To change the error reporting level to something different, pass the new level as the parameter. The value return from the function call is the old error reporting level. The following example changes the error reporting level to everything but notices and stores the old level in a variable.
The old error reporting level could then be restored at a later time:
Removing notices from the current reporting level
This final example removes E_NOTICE from the current reporting level, runs some other code, and then restores the old level back again.
The above can be tested using the following code. It sets the error reporting level to E_ALL at the start so we can be sure when testing what the initial value is:
This outputs
which is to be expected: 6143 is E_ALL and 6135 is E_ALL without E_NOTICE.
0 comments:
Post a Comment