1

I'm using this in my code but I think it can be improved and can be done a simpler way?

if($phaseOne == true && $phaseTwo == true && $phaseThree == true) {

}
Brad Fletcher
  • 3,547
  • 3
  • 27
  • 42
  • 3
    Get rid of all of the `== true` as they are not necessary – John Conde Dec 16 '16 at 13:49
  • 1
    `===` for identical comparison – Sougata Bose Dec 16 '16 at 13:51
  • Sometimes nested if's are better as you can quicker reject the whole code. Say $phaseOne is false. In your one line code it will still try all three. If you nested it with `if ($phaseOne){ if ($phaseTwo){ }}` the code will fail at the first try – Andreas Dec 16 '16 at 13:54
  • 1
    Put them in an array and iterate over them. Old programming wisdom says "2 or more, use a for" – GordonM Dec 16 '16 at 13:58

7 Answers7

7

You can do it like this:

if($phaseOne && $phaseTwo && $phaseThree) { ... }

Or use ternary operator, if you're trying to define a variable on the basis of these conditions like this:

$var = ($phaseOne && $phaseTwo && $phaseThree) ? true : false;

Hope this helps!

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
1

Assuming you have an array with an arbitrary number of logical variables:

$logical = array($phraseOne,$phraseTwo,....);
$allTrue = array_reduce($logical, function ($x,$y) {return $x && $y;},true);
if($allTrue) {
}
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
0

Do just:

if($phaseOne && $phaseTwo && $phaseThree)
Dave Plug
  • 1,068
  • 1
  • 11
  • 22
  • 1
    It's better to [add more context/explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) around code (as opposed to just having a code-only answer) as that makes the answer more useful. – EJoshuaS - Stand with Ukraine Dec 16 '16 at 17:12
0

You don't need to compare it against true.

if ($phaseOne && $phaseTwo && $phaseThree) {

}

This occurs because the result of any comparison is a boolean:

var_dump(1 == 1); // bool(true)
var_dump(1 == 2); // bool(false)

Also if you variable contains a number, it can be used directly:

if (1) {
     // This will be executed
}

if (0) {
     // This will not be executed
}

Zero will be always be treated as false, any other number (positive or negative) will be true.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59
0

Unless you need to check each variable as explicitly identical to a boolean or variable, (see this stack overflow thread)

I'd do it this way

if ($phaseOne && $phaseTwo && $phaseThree) {}

Otherwise, I'd do it this way

if ($phaseOne === true && $phaseTwo === true && $phaseThree === true) {}

Community
  • 1
  • 1
0

Try with this:

 ($phaseOne && $phaseTwo && $phaseThree) ? {//do something} : '';
LF00
  • 27,015
  • 29
  • 156
  • 295
0

Although I think it is arbitrary, controlversial, trivial, and won't preach the use of this, just for fun and learning - here's some typical php variable type juggling that will work also and takes up the least space... efficient in terms of source code length.

if($phaseOne*$phaseTwo*$phaseThree) { ... }
Werner
  • 449
  • 4
  • 12
  • the difference is that all values need to accessed unconditionally, whereas by using `&&` or `||` operators, you gain the efficiency of an early "short circuit" and thus less potential evaluations. – mickmackusa Jul 15 '22 at 05:26
  • See also: https://stackoverflow.com/a/3964189/2943403 – mickmackusa Jul 15 '22 at 05:36