0

As the title, Is the code below, a good practice to check if one of the variables is false?

return $isAdmin * $isLogin * $isConfirmed;

Instead of using if statement that is shown below.

if (!$isAdmin | !$isLogin | !$isConfirmed) {
    return false;
}

I'd like to know why if it's not.

AirQ
  • 39
  • 4
  • Both statements are perfectly equivalent. – Obsidian Age Jul 15 '22 at 02:49
  • Is there a specific reason why you want to multiply things together? Assuming based on your variable names that you have booleans, they will be cast to the integer `1` or `0` for `true` or `false`, which works, but you could skip the cast and just use an `&&`. Similarly, if they are ints or even numeric strings, although a Boolean cast will happen, it (arguably) reads easier as “and” instead of “times”. – Chris Haas Jul 15 '22 at 03:58
  • @Air this is worth a look https://stackoverflow.com/a/3964189/2943403 to benefit from short circuiting. – mickmackusa Jul 15 '22 at 05:34
  • 1
    && is more standard/semantic and will result in fewer evaluations due to how it short circuits. – Pikamander2 Jul 15 '22 at 05:34

0 Answers0