ijt
ijt

Reputation: 3855

How can I make php parse php.ini strictly?

The php command doesn't seem to check whether values in php.ini make sense. Here is an example:

$ echo 'memory_limit = foobar' > /tmp/php.ini
$ php -c /tmp -r 'printf("%s\n", ini_get("memory_limit"));'
foobar

How can I ask php to fail loudly when php.ini contains junk like this?

Upvotes: 1

Views: 36

Answers (1)

mario
mario

Reputation: 145512

You probably can't.

  • Ini-options are predefined with types (sort of).
  • Thus they're not checked by the ini parser.
  • Only the modules registering ini options ever actually interpret or complain about them. (They rarely do.)

In the case of memory_limit, it's predefined as

PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit)
  • Which means it's just a string.
  • And it's interpretation is left up to the standard string to long parser zend_atol
  • Somewhat of a core and reused function, thus does not interact with the error log
  • And it just cares about numbers and some optional quantifier suffix G or M e.g.
  • Anything else (garbage string) is interpreted as zero.

And for the purposes of memory_limit any 0 value is discarded. Because, you know, that wouldn't make sense for maximum memory usage.

Also PHP here simply assumes that at least one of all the possible php.inis contains some sort of valid or sane default. So that's why (I would say) there's no loud warning for this. It's just not commonly needed. Which again - my opinion - is somewhat of a valid design choice: don't overoptimize for edge cases.

Upvotes: 1

Related Questions