Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange php if statement problem

if($country == 224 || $country == 223 || $country == 39 && $zip == '' ){
    $_SESSION['sess_msg'] = "Please enter a Valid zipcode";
    header("location: $SITE_PATH?p=account.profile.name");
    exit;
}
variable   value
--------   -----
$country     224
$zip       11111

I know that $zip isn't empty, but the code executes as if it is. I even print it out to the browser in a debugging statement to verify that it has a value.

What is causing my program to act as if $zip does not have a value?

like image 479
JasonDavis Avatar asked Nov 27 '22 20:11

JasonDavis


2 Answers

The && operator has a higher precedence than the || operator. So your expression is equal to:

$country == 224 || $country == 223 || ($country == 39 && $zip == '')

The solution:

($country == 224 || $country == 223 || $country == 39) && $zip == ''
like image 132
Gumbo Avatar answered Dec 15 '22 07:12

Gumbo


Have you tried using parentheses to give order to your operations?

($country == 22 || $country == 223 || $country == 39) && ($zip == '') 
like image 39
George Stocker Avatar answered Dec 15 '22 06:12

George Stocker