Poemm
Poemm

Reputation: 109

PHP if this and either that or that, how?

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

Answers (5)

corecase
corecase

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

adlawson
adlawson

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:

  1. Makes it easier to skim-read your code (arguable)
  2. Triggers an error if you accidentally use = instead of == or ===

Update: To read about the difference between the == and === operators, head over to php == vs === operator

Upvotes: 3

ChrisK
ChrisK

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

Jared
Jared

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

Jasper
Jasper

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

Related Questions