Reputation: 109
I need help figuring out how to make this IF work. It basically checks IF first condition AND either of (second or third condition) are true.
IF ( ($a=1) && ($b=2 || $c=3) ) {
echo "match found";
};
Upvotes: 1
Views: 2946
Reputation: 1298
You should have double equal(comparison operator) signs when trying to see IF a,b and c are equal to 1,2 and 3 respectfully.
if(($a==1) && ($b==2 || $c==3))
{
echo "match found";
}
Also, in order to use proper syntax it would be better to write the IF in lowercase letters and remove the semicolon at the end of the if statement.
Upvotes: 6
Reputation: 6431
if (1 === $a && (2 === $b || 3 === $c)) {
echo 'Match found';
}
Why have I written it this way around?
When checking vars against values, putting the value first:
=
instead of ==
or ===
Update: To read about the difference between the ==
and ===
operators, head over to php == vs === operator
Upvotes: 3
Reputation: 1402
Well the issue i can see is you are assigning variables in your IF statement
IF ( ($a==1) && ($b==2 || $c==3) ) {
Might work better
Upvotes: 2
Reputation: 12524
You need to use the proper operator. Currently you are setting all your variables equal to 1, 2, 3 respectively. This causes each statement to always evaluate to true.
The correct comparisson operator is ==
Upvotes: 2
Reputation: 75993
if ( ($a == 1) && ($b == 2 || $c == 3) ) {
echo "match found";
}
You were using assignment operators (=
) rather than comparison operators (==
).
Here is a link to some PHP documentation about operators: http://php.net/manual/en/language.operators.php
Upvotes: 3