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

}
4
  • 3
    Get rid of all of the == true as they are not necessary Commented Dec 16, 2016 at 13:49
  • 1
    === for identical comparison Commented Dec 16, 2016 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 Commented Dec 16, 2016 at 13:54
  • 1
    Put them in an array and iterate over them. Old programming wisdom says "2 or more, use a for" Commented Dec 16, 2016 at 13:58

7 Answers 7

6

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!

Sign up to request clarification or add additional context in comments.

Comments

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) {
}

Comments

0

Do just:

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

1 Comment

It's better to add more context/explanation around code (as opposed to just having a code-only answer) as that makes the answer more useful.
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.

Comments

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) {}

Comments

0

Try with this:

 ($phaseOne && $phaseTwo && $phaseThree) ? {//do something} : '';

Comments

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

2 Comments

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.