Arno
Arno

Reputation: 375

Accidental error in setting php_value in .htaccess?

So when I currently view phpinfo() in PHP, my error_reporting is set to some number, which represents the sum of all the error constant values, right?
Let's say, I accidentally set the value of error_reporting to some weird value by accident in .htaccess for example:

  1. -1
  2. Value that is bigger than E_ALL (32768 for example, if I'm correct)
  3. When setting it with a string, I do a typo (**E_AL** & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED) - typo in E_ALL

What would happen, would it be set to some default value? Or from php.ini? I tried setting all the values above and they all were set in the Local Value without any problems.
I'm curious what will happen with any other value as well, will it default to php.ini or what?
Thanks!

Upvotes: 0

Views: 196

Answers (1)

Raggamuffin
Raggamuffin

Reputation: 1760

PHP uses bitwise operators to determine whether the error level should be reported.. so whatever your integers value is as binary it would bitwise & to see if the error is reported.

E.g. if E_DEPRECATED were (not saying this is accurate): 01, E_WARNING is 10, E_NOTICE is 100, using E_DEPRECATED | E_WARNING | E_NOTICE would give the binary 111, thus when an error is reported, like E_WARNING, it would take the value set, and bitwise & it to determine whether the report is displayed:

111 & 010 = True, so the error would be reported.

if you just used E_DEPRECATED | E_NOTICE, the binary is 101, so 101 & 010 = False, so the error won't be reported.

Each of these binaries can be converted to base10, 01 = 1, 10 = 2, 100 = 4 etc.

So if you were curious as to which errors would be reported with your random integer you can do:

int & E_WARNING in your PHP parser, if its True then the error will be shown, if its FALSE then the error will be ignored.

This is a good convention to learn, as its very useful when implementing logging on your application too, you can use the same system, and so you don't have to implement a long switch or if .. elseif .. block to determine whether a log level should be logged or not according to config.

  /**
   * Determines whether this message should be logged based on the provided type and the set threshold
   * @access protected
   * @param  int $type
   * @param  string   $name name of the log to check.
   * @return boolean
   */
  protected function should_log($type,$name='default') {
    return (bool) ($this->getConfig($name)->threshold & $type);
  }

so the threshold would be a binary number in your config (which uses constants for readability) then it bitwises & against the provided log type which returns True or False to determine whether to log the message to the logger by name $name... much more optimised.

Upvotes: 1

Related Questions