0

I tried to use the operators AND, OR in an IF statement in a PHP script like this:

if($user[id]=='1' AND (($_GET['f']=='f-ppas') || ($_GET['f']=='f-kua'))){
   .....
}

But the result is wrong. Can anyone help me?

1 Answers1

0

Change AND to &&

if($user['id']=='1' && (($_GET['f']=='f-ppas') || ($_GET['f']=='f-kua'))){
   .....
}

The AND operator has some precedence problems.

$this = true;
$that = false;

$truthiness = $this and $that;

$truthiness above has the value true. Why? = has a higher precedence than and. The addition of parentheses to show the implicit order makes this clearer:

 ($truthiness = $this) and $that

If you used && instead of and in the first code example, it would work as expected and be false.

Reference: 'AND' vs '&&' as operator

Community
  • 1
  • 1
Nikko Madrelijos
  • 545
  • 3
  • 10
  • Then you have problems with the logic of the IF statement. I don't understand what you are trying to achieve. update your question so we could answer it correctly :) – Nikko Madrelijos Apr 03 '14 at 01:03
  • Yes it will work but in some cases like what i stated in my examples, the `AND` Operator has precedence problems. – Nikko Madrelijos Apr 03 '14 at 01:09
  • It's quite unfair to tell that operator has some precedence problems. It's not. The precedence is well defined. So it's a programmer who uses an operator without checking its precedence who has some problems. – zerkms Apr 03 '14 at 01:14
  • Okay, wrong choice of word. The `AND` operator if used carelessly, it will generate the wrong output. :p – Nikko Madrelijos Apr 03 '14 at 01:16