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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2013848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:41:21  来源:igfitidea点击:

Uppercase Booleans vs. Lowercase in PHP

phplanguage-history

提问by Austin Hyde

When I was learning PHP, I read somewhere that you should always use the upper case versions of booleans, TRUEand FALSE, because the "normal" lowercase versions, trueand false, weren't "safe" to use.

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

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).

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

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

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

edit: Forgot to mention that this applies to NULLand nullas well.

编辑:忘了提到这也适用于NULLnull

采纳答案by Lukman

The official PHP manualsays:

官方PHP手册说:

To specify a boolean literal, use the keywords TRUE or FALSE. Both are case-insensitive.

要指定布尔文字,请使用关键字 TRUE 或 FALSE。两者都不区分大小写。

So yeah, true === TRUEand false === FALSE.

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

Personally, however, I prefer TRUEover trueand FALSEover falsefor readability reasons. It's the same reason for my preference on using ORover oror ||, and on using ANDover andor &&.

然而,就个人而言,出于可读性原因,我更喜欢TRUE一遍trueFALSE一遍false。这与我偏爱使用ORover oror||和使用ANDover andor 的原因相同&&

The PSR-2standard requires true, falseand nullto be in lower case.

PSR-2标准要求truefalse并且null为小写。

回答by Radu

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

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

调试愉快!(PHP < 5.1.3(2006 年 5 月 2 日),见演示

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)

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

提交d51599dfcd3282049c7a91809bb83f665af23b69(和6f76b17079a709415195a7c27607cd52d039d7c3

回答by Bo Allen

Use lowercase.

使用小写。

  1. It's easier to type. (IMO)
  2. It's easier to read. (IMO)
  3. JavaScript booleans are lowercase and case-sensitive.
  1. 打字更容易。(海事组织)
  2. 阅读起来更容易。(海事组织)
  3. JavaScript 布尔值是小写且区分大小写的。

回答by Mandrake

If you intend to use JSON, then RFC7159says:

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

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

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

From the list of backward incompatible changes in PHP 5.6:

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

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

According to PSR-2 standard:

根据PSR-2 标准

PHP keywords MUST be in lower case.

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

PHP 关键字必须小写。

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

回答by Todd

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

我曾经使用 C 风格的 TRUE/FALSE 布尔值,就像所有常量一样,全部大写,直到我加入PSR潮流。

Section 2.5 of PSR-2:

PSR-2 第 2.5 节:

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

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

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

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

回答by Tatu Ulmanen

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

没关系,true完全一样TRUE。这同样适用于falsenull。我没有听说这在任何时候都很重要。

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.

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

回答by Arkadij Kuzhel

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

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

    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.

所以我们可以看到布尔情况对性能的影响很小 - 小写更快。但你肯定不会看到。

回答by RMcLeod

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.

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

回答by Conrad

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.

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

回答by Abbas Uddin

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

这是我在 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.

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