开发者

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";
};


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.


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜