views:

371

answers:

3

When using error_reporting() or ini_set('error_reporting') in my scripts, are there any functionality differences between the two? Is one method preferred over the other?

For what it's worth, I see many frameworks using error_reporting(), but both options appear to be set during runtime only, then reset back to their default in php.ini after script execution.

+1  A: 

They are functionally identical, but if you are using an IDE that knows the PHP function names, this is an easy way to make sure you don't accidentally mistype the name of the directive you are tying to set.

From the examples section on PHP's Manual Entry for error_reporting():

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
Dereleased
Bah, my IDE (notepad++) gets 'em both wrong. Good input, though.
Jeff
In recent times (that is, for the last six years or so) I've only used Komodo (90%) and jEdit, and they both know these functions.
Dereleased
+3  A: 

The only small functional difference seems to be that ini_set returns false when it was unable to change the setting and error_reporting always returns the old error level.

Pekka
Not exactly; ini_set returns false when the setting doesn't exist. It expects a string, so anything that can be interpreted as a string is converted. If you pass it an Object, for example, PHP generates a warning and it returns null, which is identical to the behavior of error_reporting when passed an object (an object without a __toString() method, that is). Point being, his question was the difference between `ini_set('error_reporting')` and `error_reporting()`, not the way in which `ini_set()` handles errors in directive names.
Dereleased
I should clarify, when I say "it expects a string" I am referring to the second parameter, the value of the setting being set, and everything else refers to that.
Dereleased
@Dereleased, I think that ini_set's returning false is based in the possibility of blocking the changing of some settings at runtime in `php.ini`. So I would assume that if changing the error level at runtime is disabled in php.ini (Not sure whether that's possible but I think it is) ini_set() will return `false` while `error_reporting`will, at least according to the manual, always return the old error level.
Pekka
+3  A: 

"Two roads leading you to Rome": ini_set('error_reporting', ) overrides the parameter set in the php.ini file. error_reporting() receives level number or level id

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

Both options take effect until the script ends its execution. The next one will use the params defined in the .ini, again.

Alfabravo
+1 because I can't accept two answers.
Jeff