Uppercase Booleans vs. Lowercase in PHP

2019-01-30 07:11发布

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.

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?

edit: Forgot to mention that this applies to NULL and null as well.

10条回答
迷人小祖宗
2楼-- · 2019-01-30 07:38

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-30 07:46

The official PHP manual says:

To specify a boolean literal, use the keywords 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.

查看更多
祖国的老花朵
4楼-- · 2019-01-30 07:48

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.

查看更多
Fickle 薄情
5楼-- · 2019-01-30 07:50

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.

查看更多
混吃等死
6楼-- · 2019-01-30 07:59

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.

查看更多
放我归山
7楼-- · 2019-01-30 08:00

Use lowercase.

  1. It's easier to type. (IMO)
  2. It's easier to read. (IMO)
  3. JavaScript booleans are lowercase and case-sensitive.
查看更多
登录 后发表回答