blips
blips

Reputation: 41

IF statement with multiple conditions

I can’t get the desired result from a series of conditions in an IF.

if (($varteam == $_POST['rteam1']) && ($varteam == $_POST['rteam2']) && ($varteam == $_POST['rteam3'])  && ($varteam == $_POST['rteam4']) && ($varteam == $_POST['rteam5'])) 
{true} 
  else
{false}

Starting from the variable $varteam I want to obtain true if all the compared values are identical, otherwise false.

The compared values may also be null.

With the code I’ve posted it works if all the values are equal or different but I get true instead of false if one or more values are different.

Why does it happen?

Upvotes: 0

Views: 73

Answers (1)

Sutandiono
Sutandiono

Reputation: 1750

I am guessing that you may get false positives when you have 0 mixed with null or false. Just to be on the safe side, use === instead of == so type checking is in effect. That way, null !== false !== 0.

if (($varteam === $_POST['rteam1']) &&
    ($varteam === $_POST['rteam2']) &&
    ($varteam === $_POST['rteam3']) &&
    ($varteam === $_POST['rteam4']) &&
    ($varteam === $_POST['rteam5'])) 
{
   // true
} 
else
{
   // false
}

Upvotes: 1

Related Questions