Uppercase Booleans vs. Lowercase in PHP

PhpLanguage History

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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 Wed, 15 Mar 2006 09:04:48 +0000 (09:04 +0000) committer dmitry Wed, 15 Mar 2006 09:04:48 +0000 (09:04 +0000) commit d51599dfcd3282049c7a91809bb83f665af23b69 tree 05b23b2f97cf59422ff71cc6a093e174dbdecbd3 parent a623645b6fd66c14f401bb2c9e4a302d767800fd

> Commits d51599dfcd3282049c7a91809bb83f665af23b69 (and 6f76b17079a709415195a7c27607cd52d039d7c3)

Solution 2 - Php

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.

Solution 3 - Php

Use lowercase.

  1. It's easier to type. (IMO)
  2. It's easier to read. (IMO)
  3. JavaScript booleans are lowercase and case-sensitive.

Solution 4 - Php

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.

Solution 5 - Php

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.

Solution 6 - Php

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.

Solution 7 - Php

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.

Solution 8 - Php

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.

Solution 9 - Php

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.

Solution 10 - Php

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.

Solution 11 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAustin HydeView Question on Stackoverflow
Solution 1 - PhpRaduView Answer on Stackoverflow
Solution 2 - PhpLukmanView Answer on Stackoverflow
Solution 3 - PhpBo AllenView Answer on Stackoverflow
Solution 4 - PhpMandrakeView Answer on Stackoverflow
Solution 5 - PhpToddView Answer on Stackoverflow
Solution 6 - PhpTatu UlmanenView Answer on Stackoverflow
Solution 7 - PhpArkadij KuzhelView Answer on Stackoverflow
Solution 8 - PhpRMcLeodView Answer on Stackoverflow
Solution 9 - PhpConradView Answer on Stackoverflow
Solution 10 - PhpAbbas UddinView Answer on Stackoverflow
Solution 11 - PhpJanView Answer on Stackoverflow