dsgw8802 2015-12-26 01:13
浏览 337
已采纳

为什么PHP var_export()打印NULL大写,但是true和false小写?

See https://3v4l.org/6Oelk

var_export(array(NULL, TRUE, FALSE));

Result:

array (
  0 => NULL,
  1 => true,
  2 => false,
)

NULL is uppercase, but true and false are lowercase.

I suppose this has a historic reason, but I don't find it documented anywhere.


The following experiment is also quite interesting, and suggests that internally the "canonical" spelling of null, true and false is lowercase, contrary to what var_export() does: https://3v4l.org/ggM4E

define('false', '- false -');
define('FALSE', '- FALSE -');
define('null', '- null -');
define('NULL', '- NULL -');
var_export(array(TRUE, true, FALSE, false, NULL, null));

It shows that:

  • define('NULL', ..) and define('FALSE', ..) are allowed but have no effect in recent PHP versions.
  • define('null', ..) and define('false', ..) give a notice, but also have no effect in recent PHP versions.
  • In older PHP version (4.x), 'NULL' and 'FALSE' CAN be redefined and it does have an effect. But 'null' and 'false' cannot be redefined.

Oh, just for completeness: define('NULL', ..) and define('FALSE', ..) are actually NOT ignored if within a namespace, in PHP 5.x. See https://3v4l.org/JusvB. Or if you really want, you can even override 'null' and 'false' within a namespace: https://3v4l.org/i796C


Related: Uppercase Booleans vs. Lowercase in PHP, see this answer: https://stackoverflow.com/a/3807178/246724

  • 写回答

1条回答 默认 最新

  • douhan6738 2015-12-30 20:31
    关注

    As @NikiC notes in the OP comments, there's no specific reasoning for this.

    The most logical explanation though would be that considering PHP is written in C and historical standards define NULL as uppercase, and true, false as lowercase, reason follows that the standards in PHP would follow suit.

    IEEE Std 1003.1 References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误