0

As in the title of the question - should I use $variable == false or !$variable when checking the value in some if? I know that they're basically doing the same thing and the second version is faster for some reason, but I'm asking about best practices.

There was a similar question here: Exclamation mark in front of variable - clarification needed but my question is different. I'm aware that both ways are doing the same thing. I'm asking if there's any technical detail or best practice that would make one of them preferable.

Community
  • 1
  • 1
Bartosz Górski
  • 1,140
  • 13
  • 30
  • 1
    Just preference. Choose one and stick with it. The latter is more concise but a little harder to read, so if you go with that option make sure your variables are well named – Steve Aug 09 '16 at 16:19
  • 1
    It's more about the readability. Choose the one you like. – B001ᛦ Aug 09 '16 at 16:19
  • Possible duplicate of [Exclamation mark in front of variable - clarification needed](http://stackoverflow.com/questions/13029921/exclamation-mark-in-front-of-variable-clarification-needed) – Pedro Lobito Aug 09 '16 at 16:23
  • 3
    `$variable===false` is actually best if you're expecting your variable to be a boolean, because when it fails because the variable is a non-boolean (even if it's falsey like the empty string `''`), you will notice the unexpected behavior right away and spot errors early. – BeetleJuice Aug 09 '16 at 16:23
  • I'd code for readability first. One additional comment is that using ! is much shorter and can help keep your lines under 80 characters long. Especially if the condition body uses a method `if (!$this->longMethodName())` – Darren Felton Sep 01 '16 at 19:07

0 Answers0