1

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";
};
Poemm
  • 109
  • 2
  • 6

5 Answers5

6

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.

corecase
  • 1,278
  • 5
  • 18
  • 29
  • while the capitalized IF and stray semicolon are both bad style, neither causes an actual functional issue. – Frank Farmer Aug 24 '11 at 00:40
  • So you're saying it's better not to let the guy know? Honestly i don't see why this answer deserves two downvotes... – corecase Aug 24 '11 at 00:41
  • It's worth mentioning those, but only after explaining the more important functional issue: confusing the assignment and comparison operators. – Frank Farmer Aug 24 '11 at 00:43
  • i understand semi column i don't like it myself but why not capitalize if it stands out better to me when capital – Poemm Aug 24 '11 at 00:45
  • @Poemm it's okay to capitalize, it just doesn't follow the common tradition that most people follow. – corecase Aug 24 '11 at 00:47
  • 1
    I believe only lowercase `if` is valid in most C-like languages. PHP is the exception, being much looser with case sensitivity than most of its siblings. That's why many programmers (who often have experience with multiple C-like languages) will favor lowercase keywords like `if`. – Frank Farmer Aug 24 '11 at 00:51
3
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

Jasper
  • 75,717
  • 14
  • 151
  • 146
3
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

Community
  • 1
  • 1
adlawson
  • 6,303
  • 1
  • 35
  • 46
  • +1 for "yoda conditions", but the use of `===` could use some explanation -- if $a $b or $c contain string values, `==` might be more appropriate. – Frank Farmer Aug 24 '11 at 00:46
  • @Frank Ha, "yoda conditions". Never heard them called that before. *Like it I do* – adlawson Aug 24 '11 at 00:51
  • If you are truly all about clarity, then why not write it as ((1 === $a) && ((2 === $b) || (3 === $c))? It helps when reading code at 3am. – Gabriel Magana Aug 24 '11 at 02:11
  • @gabriel ...because that's kinda overkill. Putting braces around everything doesn't make code clarity any better. Even at 3am. – adlawson Aug 24 '11 at 06:53
2

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 ==

Jared
  • 12,406
  • 1
  • 35
  • 39
2

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

ChrisK
  • 1,392
  • 10
  • 12