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:

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

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了