简体   繁体   English

如何让 php 严格解析 php.ini?

[英]How can I make php parse php.ini strictly?

The php command doesn't seem to check whether values in php.ini make sense. php 命令似乎没有检查 php.ini 中的值是否有意义。 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?当 php.ini 包含这样的垃圾时,如何让 php 大声失败?

You probably can't.你可能不能。

  • Ini-options are predefined with types (sort of). Ini 选项是用类型(某种)预定义的。
  • Thus they're not checked by the ini parser.因此它们不会被 ini 解析器检查。
  • Only the modules registering ini options ever actually interpret or complain about them.只有注册 ini 选项的模块才会真正解释或抱怨它们。 (They rarely do.) (他们很少这样做。)

In the case of memory_limit, it's predefined as在 memory_limit 的情况下,它被预定义为

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它的解释留给了标准字符串到长解析器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 eg它只关心数字和一些可选的量词后缀GM例如
  • Anything else (garbage string) is interpreted as zero.其他任何东西(垃圾字符串)都被解释为零。

And for the purposes of memory_limit any 0 value is discarded.并且出于memory_limit的目的,任何 0 值都将被丢弃。 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.ini s contains some sort of valid or sane default.此外,这里的 PHP 只是假设所有可能的php.ini中至少有一个包含某种有效或合理的默认值。 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.这再次 - 我的意见 - 是一种有效的设计选择:不要对边缘情况进行过度优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM