152

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?

6
  • 4
    There is a number of sites saying that writing it lowercase is supposedly "much faster", but without citing any serious sources. Interested to see whether anything comes up.
    – Pekka
    Commented Jan 6, 2010 at 15:14
  • 3
    That's not true. I tested it with 5M iterations and they both gave the same results, 0.5s on my PC :P
    – Alex
    Commented May 29, 2012 at 2:22
  • 9
    @Alex Pekka may also mean that the actual writing of lowercase booleans is faster. That makes sense, having to use less keys. The difference is tiny, though. Commented Nov 8, 2012 at 16:51
  • Sorry, I accidentally voted down after after I voted up for you.
    – SIFE
    Commented Jun 20, 2013 at 10:37
  • 2
    An interesting observation: var_export() writes true and false as lowercase, but NULL as uppercase. Lovely, eh? 3v4l.org/6Oelk
    – donquixote
    Commented Dec 26, 2015 at 0:33

11 Answers 11

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

9
  • 8
    -1 because A) this is pointless. B) this doesn't answer the question. and C) I've already accepted the correct answer, and this doesn't present any additional helpful information. Commented Sep 27, 2010 at 22:47
  • 37
    Then let me explain in detail: uppercase bools are constants and lowercases are values. You are interested in the value, not in the constant, which can easily change. So, if you would have thought a little on the text and not rush to give a penalty, you probably would have understood this.
    – Radu
    Commented Sep 28, 2010 at 6:00
  • 14
    I understand the point you are making (now), but the way you originally made it was (IMO) cryptic and pointless. Had you originally just made your edit the answer, I would have upvoted, as this is a very good point, actually. Commented Sep 28, 2010 at 22:36
  • 4
    This is the right answer. Should have gotten the points for this.
    – serialworm
    Commented Jun 23, 2011 at 19:49
  • 27
    As for the edit, that description is a bit misleading... The actual values themselves (which get compiled down into tokens T_NULL, T_TRUE, and T_FALSE at parse time), are case-insensitive, so using NULL is not actually a "constant" --- unless you make it a constant, using define(). Simply using NULL or TRUE does not mean it's a constant, as if there is no such constant, PHP interprets it as the literal. A more accurate description is that the lowercase versions cannot be redefined, whereas any other case variation can.
    – Joe
    Commented Dec 31, 2012 at 17:38
102

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.

11
  • 94
    Just want to point out that OR and || are different operators in PHP (as in they have different precedence), as are AND and &&. (So for example && and || are higher precedence than the assignment operators, but OR and AND are not.)
    – Johrn
    Commented Jun 10, 2010 at 17:25
  • 22
    Also with today's IDE's I do not see the reason to have pure upper-case boolean as the Syntax Highlighter for most IDE's separate them with great distinction.
    – RobertPitt
    Commented Sep 28, 2010 at 9:51
  • 43
    I much prefer typing them lowercase so I don't have to hold down the shift key.
    – ack
    Commented Apr 7, 2012 at 21:12
  • 8
    I like lower case as well--like in java, but to each there own. I'd wish however they'd settle on ONE way and just make us all change over to one or the other!!!!! This isn't a flexibility I really need to have!
    – Ray
    Commented Oct 24, 2012 at 15:42
  • 5
    @Cyril: There are plenty of good reasons to indulge in hating PHP (e.g. just see what Johrn mentioned above, I didn't know that for one), but given that it's basically case-insensitive, there is nothing strange in mixed-case expressions being the same. What is nonsense, however, that it mixes case-sensitivity and case-insensitivity with general insensibility. (See e.g.: stackoverflow.com/questions/5643496/…)
    – Sz.
    Commented Jun 19, 2013 at 10:29
35

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
  • 7
    +1; I was just about adding the Javascript argument: as it's VERY common in web programming to write both PHP and JS code, well developed web development finger muscles continue to routinely apply the same letter case used in the last language context. At least I often found myself writing TRUE or FALSE in Javascript after switching from PHP. Using lower case in PHP fixed this one for good.
    – Sz.
    Commented Jun 19, 2013 at 10:45
13

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.

3
  • Correct, but this question was specifically in regards to upper/lowercase booleans in PHP, not JSON. For example, both json_encode(TRUE) and json_encode(true) yield 'true'. Commented Jan 26, 2015 at 20:21
  • 3
    Sorry for mentioning JSON, would it be better if I removed all those references and only mentioned PSR-2 ?
    – Mandrake
    Commented Jan 27, 2015 at 19:54
  • 6
    I think the reference to JSON is appropriate, as you frequently use javascript/JSON with PHP, and may be looking for consistency.
    – Doug Kress
    Commented Aug 22, 2015 at 19:15
11

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.

7

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.

2
  • FALSE and NULL are not the same. is_null() does not return true if the value === FALSE. Commented Nov 23, 2010 at 21:28
  • 10
    @Noah Goodrich, I did not at any point imply that false and null would be the same. I said that "same goes for false and null", which meant that both can be expressed in lower or uppercase letters. Commented Nov 24, 2010 at 10:09
5

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.

2
  • What JS engine? Nowadays JS is compiled in memory before execution.
    – oxygen
    Commented May 21, 2013 at 15:00
  • 1
    PHP is tokenized before execution, and there should be no difference in time. Another answer indicated that upper was faster. Differences of this magnitude in any test should be ignored - this difference is 2.5e-7 per iteration.
    – Doug Kress
    Commented Aug 22, 2015 at 19:22
4

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.

1
  • 2
    +1 for tidy-ness. All caps tradition comes from C, but it's about time we get rid of that ugly form.
    – Petruza
    Commented Aug 30, 2012 at 18:10
2

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.

2
  • 6
    Just for future reference, you posted this as an "answer". On StackOverflow, unlike traditional forums, conversational posts that don't actually serve as a solution to the question and don't provide helpful information should be posted as a comment to either the original question or the relevant answer. In this case, your post would be better suited to a comment on my question than an answer. Commented Jan 17, 2012 at 6:18
  • 8
    actually it's helpful to know this doesn't work anymore in PHP 5 :P
    – Alex
    Commented May 29, 2012 at 2:19
1

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.

1

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

1
  • They're completely case-insensitive, so tRuE, tRUe, etc are also valid (although I wouldn't recommend using them). Commented Jun 22 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.