为什么在strict_types=1中使用整数1,而不是布尔值true?

huangapple go评论60阅读模式
英文:

Why we have integer 1 in strict_types=1 instead of boolean true?

问题

我知道要强制使用标量类型时,我们使用

declare(strict_types=1)

但我的问题是,为什么使用整数1?它代表了TRUE的布尔值,还是有其他不同的含义?

英文:

I know that to enforce the scalar types, we use

declare(strict_types=1)

But my question is, why use the integer 1? Is it representing TRUE's boolean value, or does it have to do something different?

答案1

得分: 2

PHP是用C语言编写的。1999年之前的旧版本C语言不支持布尔数据类型。PHP的第一个版本是在1994年开发的。可能的原因是为了向后兼容性。在PHP中通常没有布尔数据类型的配置变量。您可以在https://www.php.net/manual/en/ini.list.php上找到更多信息。

英文:

PHP is written in C. Older versions of C before 1999 did not support boolean data type. First version of PHP was developed in 1994. The probable reason is backwards compatibility. There are generally no configuration variables in PHP in boolean data type https://www.php.net/manual/en/ini.list.php

答案2

得分: 1

有关这个功能,有一个相关的 RFC。根据这个 RFC,strict_types 允许的值是 0 和 1,对应 0=弱类型,1=严格类型,因此实际上它们本质上是布尔值。

那么为什么使用 1 而不是 true 呢?似乎没有一个实际的答案。已实施的 RFC 是从这个 RFC进行了分支,两者都指定了 strict_types 的值为 0 或 1,分别对应禁用和启用。

为什么要做出这个决定,即为什么使用整数值而不是布尔值,没有解释。在 declare 中可以用于新功能的内容没有受到限制的文档记录。declare 自 PHP 版本 4 就存在,因此我猜测可能是因为(a)它使实施更容易,或者(b)它遵循一种约定。

英文:

There is a relevant RFC for this feature. According to this RFC the values permissible for strict_types are 0 and 1 corresponding 0=weak types 1=strict types so in practice these are essentially boolean values.

Now why use 1 and not true? There doesn't seem to be an actual answer to this question. The implemented RFC was forked from this one both of which specify the use of strict_types values of 0 or 1 corresponding to disabled and enabled.

There is no explanation for why this decision was made i.e. why use the integer values instead of boolean values. There is no restrictions documented in what can be used in declare for new features. Declare has been in PHP since v4 so if I had to guess I would think it's either (a) because it makes implementation easier or (b) it follows a convention.

huangapple
  • 本文由 发表于 2023年3月1日 09:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定