Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top