33

I'm wondering, in PHP, I've seen people use both these cases:

if($var === TRUE)

if($var === true)

is there an actual difference between them or a coding standard/format to be used in boolean value?

Reslan Tinawi
  • 448
  • 4
  • 12
mahen3d
  • 7,047
  • 13
  • 51
  • 103
  • 7
    No difference. If you want to follow the [PSR-2 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md), they must be in lower case. – Federkun Jul 18 '12 at 10:47
  • What about _True_ and _False_? I've seen that being used and strangely the code was supposed to work, but I doubt it... – Gwyneth Llewelyn Nov 23 '18 at 20:25
  • Guess, it's related with getting used to another language before becoming a PHP developer. For example in python docs, it says `True` and `False`, while in C++ you define `TRUE` and `FALSE` as constants assigned to 1 and 0 (if you don't already use `stdbool.h` and obey the "constants should be uppercase" rule). – Taha Paksu Dec 27 '19 at 12:00

2 Answers2

53

There's no difference at all. From the docs:

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

According to PSR-2, as stated by both Laxus and Paul Bain in their comments, the standard is to write them in lower case.

Andrei
  • 19
  • 1
  • 3
Diego Agulló
  • 9,298
  • 3
  • 27
  • 41
  • 3
    for Developers who will read this in 2020year :) PSR-2 is deprecated now you can read PSR-12 2.5 https://www.php-fig.org/psr/psr-12/ – Davit Huroyan Jan 14 '20 at 10:20
12

lowercase is generally considered a good practice, see the coloration of the lowercase on stackoverflow :)

And not related, but you should always use true first in the condition true === $var, this is a good practice to avoid sneaky bugs when mistyping the condition, eg : $var = true

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Baptiste Placé
  • 386
  • 2
  • 5