14

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.

4 Answers 4

10

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.

3
  • 4
    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
    Commented Dec 23, 2009 at 20:51
  • 1
    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
    Commented Dec 23, 2009 at 20:56
  • @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_reportingwill, at least according to the manual, always return the old error level.
    – Pekka
    Commented Dec 23, 2009 at 21:15
8

"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.

0
4

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);
2
  • 1
    Bah, my IDE (notepad++) gets 'em both wrong. Good input, though.
    – Jeff
    Commented Dec 23, 2009 at 20:41
  • 1
    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
    Commented Dec 23, 2009 at 20:54
1

Also, even though the docs stated that the signature for error_reporting is:

int error_reporting ([ int $level ] )

, it's not exactly correct because you can set a string and read it back with ini_get:

error_reporting('123 hello world');

var_dump(ini_get('error_reporting'));

produces:

string(15) "123 hello world"

So, error_reporting($x) is semantically equivalent to ini_set('error_reporting', $x),

and error_reporting() is semantically equivalent to (int)ini_get('error_reporting').

Not the answer you're looking for? Browse other questions tagged or ask your own question.