PHP 中的大写布尔值/Null 与小写字母

发布于 2024-08-16 23:35:28 字数 309 浏览 4 评论 0原文

当我学习 PHP 时,我在某处读到,您应该始终使用布尔值的大写版本 TRUEFALSE,因为“正常”小写版本 truefalse 使用起来并不“安全”。这也适用于 NULLnull

现在已经很多年了,我编写的每个 PHP 脚本都使用大写版本。但现在我对此表示质疑,因为我已经看到很多用小写版本(即 Zend Framework)编写的 PHP。

是否有理由使用大写版本,或者使用小写版本完全可以吗?

When I was learning PHP, I read somewhere that you should always use the upper case versions of booleans, TRUE and FALSE, because the "normal" lowercase versions, true and false, weren't "safe" to use. This applies to NULL and null as well.

It's now been many years, and every PHP script I've written uses the uppercase version. Now, though, I am questioning that, as I have seen plenty of PHP written with the lowercase version (i.e. Zend Framework).

Is/Was there ever a reason to use the uppercase version, or is it perfectly OK to use the lowercase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

噩梦成真你也成魔 2024-08-23 23:35:28
define('TRUE', false);
define('FALSE', true);

调试愉快! (PHP 5.1.3(2006 年 5 月 2 日),请参阅 < a href="http://3v4l.org/Lp0vc" rel="noreferrer">演示)

编辑: 大写布尔值是常量,小写布尔值是值。您感兴趣的是值,而不是常数,因为常数很容易改变。


消除了 TRUE、FALSE 和 NULL 的运行时常量获取

作者德米特里
            2006 年 3 月 15 日星期三 09:04:48 +0000 (09:04 +0000)
提交者 dmitry ;
            2006 年 3 月 15 日星期三 09:04:48 +0000 (09:04 +0000)
提交d51599dfcd3282049c7a91809bb83f665af23b69
树 05b23b2f97cf59422ff71cc6a093e174dbdecbd3
父级 a623645b6fd66c14f401bb2c9e4a302d767800fd

提交d51599dfcd3282049c7a91809bb83f665af23b69 < a href="http://git.php.net/?p=php-src.git;a=commit;h=6f76b17079a709415195a7c27607cd52d039d7c3" rel="noreferrer">6f76b17079a709415195a7c27607cd52d039d7c3)

define('TRUE', false);
define('FALSE', true);

Happy debugging! (PHP < 5.1.3 (2 May 2006), see Demo)

Edit: Uppercase bools are constants and lowercases are values. You are interested in the value, not in the constant, which can easily change.


Eliminated run-time constant fetching for TRUE, FALSE and NULL

author      dmitry <dmitry>
            Wed, 15 Mar 2006 09:04:48 +0000 (09:04 +0000)
committer   dmitry <dmitry>
            Wed, 15 Mar 2006 09:04:48 +0000 (09:04 +0000)
commit      d51599dfcd3282049c7a91809bb83f665af23b69
tree        05b23b2f97cf59422ff71cc6a093e174dbdecbd3
parent      a623645b6fd66c14f401bb2c9e4a302d767800fd

Commits d51599dfcd3282049c7a91809bb83f665af23b69 (and 6f76b17079a709415195a7c27607cd52d039d7c3)

沒落の蓅哖 2024-08-23 23:35:28

官方 PHP 手册 说:

要指定布尔文字,请使用
常量truefalse。两者都是
不区分大小写。

所以是的,true === TRUEfalse === FALSE

但就我个人而言,出于可读性的原因,我更喜欢 TRUE 而不是 true,更喜欢 FALSE 而不是 false。这与我更喜欢使用 OR 而不是 or|| 以及使用 AND 而不是 < 的原因相同。代码>和或<代码>&&。

PSR-2 标准要求 true,< code>false 和 null 为小写。

The official PHP manual says:

To specify a bool literal, use the
constants true or false. Both are
case-insensitive.

So yeah, true === TRUE and false === FALSE.

Personally, however, I prefer TRUE over true and FALSE over false for readability reasons. It's the same reason for my preference on using OR over or or ||, and on using AND over and or &&.

The PSR-2 standard requires true, false and null to be in lower case.

雨的味道风的声音 2024-08-23 23:35:28

使用小写。

  1. 打字更容易。 (IMO)
  2. 它更容易阅读。 (IMO)
  3. JavaScript 布尔值是小写且区分大小写的。

Use lowercase.

  1. It's easier to type. (IMO)
  2. It's easier to read. (IMO)
  3. JavaScript booleans are lowercase and case-sensitive.
放手` 2024-08-23 23:35:28

如果您打算使用 JSON,则 RFC7159 表示:

文字名称必须小写。不允许使用其他文字名称。

PHP 5.6 中向后不兼容的更改列表中:

json_decode() 现在根据 JSON 规范始终拒绝 JSON 文字 true、false 和 null 的非小写变体

根据 PSR-2 标准

PHP 关键字必须小写。

PHP 常量 true、false 和 null 必须小写。

If you intend to use JSON, then RFC7159 says:

The literal names MUST be lowercase. No other literal names are allowed.

From the list of backward incompatible changes in PHP 5.6:

json_decode() now rejects non-lowercase variants of the JSON literals true, false and null at all times, as per the JSON specification

According to PSR-2 standard:

PHP keywords MUST be in lower case.

The PHP constants true, false, and null MUST be in lower case.

乖乖哒 2024-08-23 23:35:28

我曾经像所有 const 一样执行 C 风格的 TRUE/FALSE 布尔值,全部大写,直到我进入 PSR 潮流。

PSR-2 第 2.5 节:

PHP 常量 true、false 和 null 必须小写。

所以基本上,如果你想很好地处理开源风格的细节,布尔值必须是小写的。

I used to do C style TRUE/FALSE booleans like all consts, in all caps, until I got on the PSR bandwagon.

Section 2.5 of PSR-2:

The PHP constants true, false, and null MUST be in lower case.

So basically, if you want to play nice with open source style particulars, Booleans gotta be lower case.

暖风昔人 2024-08-23 23:35:28

没关系,trueTRUE 完全相同。 falsenull 也是如此。我没听说过这在任何时候都会很重要。

搞砸事情的唯一方法是引用这些值,例如:

$foo = false;   // FALSE
$bar = "false"; // TRUE

$foo2 = true;   // TRUE
$bar2 = "true"; // TRUE

$foo3 = null;   // NULL
$bar3 = "null"; // TRUE

唯一限制或鼓励您使用大写或小写的东西可能是您公司或您自己的编码指南。除此之外,您可以自由使用其中任何一种,并且不会导致任何问题。

It doesn't matter, true is exactly the same as TRUE. Same goes for false and null. I haven't heard that it would have mattered at any point.

The only way you can mess things up is by quoting those values, for example:

$foo = false;   // FALSE
$bar = "false"; // TRUE

$foo2 = true;   // TRUE
$bar2 = "true"; // TRUE

$foo3 = null;   // NULL
$bar3 = "null"; // TRUE

Only thing restricting or encouraging you to use upper or lowercase might be your company's or your own coding guidelines. Other than that, you're free to use either one and it will not lead in any issues.

探春 2024-08-23 23:35:28

我编写了简单的代码来检查 falseFALSE 之间的差异:
每次迭代都在做以下事情:

    for ($i = 0; $i < self::ITERATIONS; ++$i) {
       (0 == FALSE) ;
    }

以下是结果:

Iterations: 100000000
using 'FALSE': 25.427761077881 sec
using 'false': 25.01614689827 sec

因此我们可以看到,布尔值大小写对性能的影响非常小 - 小写字母更快。但你肯定不会看到。

I've written simple code to check the differences between false and FALSE:
Each iteration was doing something that:

    for ($i = 0; $i < self::ITERATIONS; ++$i) {
       (0 == FALSE) ;
    }

Here are the results:

Iterations: 100000000
using 'FALSE': 25.427761077881 sec
using 'false': 25.01614689827 sec

So we can see that performance is very slightly touched by the booleans case - lowercase is faster. But certainly you won't see.

只有一腔孤勇 2024-08-23 23:35:28

就我个人而言,我一直使用小写形式,但除了为了让我的代码看起来整洁之外没有什么特殊原因,我使用大写字母的唯一地方是当类名和变量名采用驼峰式大小写时。

不过,我想到的使用大写字母的一个优点是它们很突出并且很容易在代码中找到。

Personally I've always used the lowercase form, but for no particular reason other than to make my code look tidy, the only place I use capital letters is when camel casing class names and variable names.

One advantage to using uppercase that comes to mind though is that they stick out and are easy to find in code.

荆棘i 2024-08-23 23:35:28

我在问自己同样的事情时遇到了这个老问题。
好点是define('TRUE', false);define('FALSE', true);
但不适用于 php5。在 php5 代码中编写这些行就像编写注释一样。

I came across this old question while asking myself the same thing.
Good point with define('TRUE', false);define('FALSE', true);
Doesn't apply to php5 though. Writing those lines in a php5 code is like writing a comment.

酒废 2024-08-23 23:35:28

这是我在 Windows 7x64bit Apache/2.4.9 PHP/5.5.14 上的测试

$blockLimit = 50;
while($blockLimit > 0): $blockLimit--;

//STAR Here ================================================

$msc = microtime(true);
for ($i = 0; $i < 100000; $i++) {
   echo (FALSE);
}
echo 'FALSE took ' . number_format(microtime(true)-$msc,4) . " Seconds\r\n";
$msc = microtime(true);
for ($i = 0; $i < 100000; $i++) {
   echo (false);
}
echo 'false took ' . number_format(microtime(true)-$msc,4) . " Seconds\r\n";

echo "\r\n --- \r\n";
//Shutdown ==================================================
endwhile;

这次 FALSE 赢了 20 次。所以在我的环境中大写字母更快。

Here is my TEST on Windows 7x64bit Apache/2.4.9 PHP/5.5.14

$blockLimit = 50;
while($blockLimit > 0): $blockLimit--;

//STAR Here ================================================

$msc = microtime(true);
for ($i = 0; $i < 100000; $i++) {
   echo (FALSE);
}
echo 'FALSE took ' . number_format(microtime(true)-$msc,4) . " Seconds\r\n";
$msc = microtime(true);
for ($i = 0; $i < 100000; $i++) {
   echo (false);
}
echo 'false took ' . number_format(microtime(true)-$msc,4) . " Seconds\r\n";

echo "\r\n --- \r\n";
//Shutdown ==================================================
endwhile;

This time FALSE won 20 times. So uppercase is faster in my environment.

心房的律动 2024-08-23 23:35:28

我参加聚会已经晚了好几年,但我想提一些有趣的事情,但还没有出现在主题中。今天我发现 True 也是有效的,而不仅仅是 trueTRUE。所有拼写都是等效的。这是相关的,因为 PHP 的 Open API 生成器使用 True。 (这让我陷入了一种困惑的心态,并进行了搜索,找到了这个页面)。

I am years late to the party but wanted to mention something interesting that isn't in the thread yet. Today I discovered True is also valid, not just true or TRUE. All spellings are equivalent. This is relevant because of the Open API generator for PHP, it uses True. (Which led me to a bewildered state of mind and a search which found this page).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文