views:

294

answers:

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

A: 

What does it do if you change it to this?

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

I am curious if the types of the variables are causing a problem here, === will compare both the value and the type of the variable.

Andrew Hare
+15  A: 

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

($country == 22 || $country == 223 || $country == 39) && ($zip == '')
George Stocker
A: 

Yea... but are they set to one of the correct values? Country is 224 or 223 or 39 and zip is a blank string? I want to stress, zip is a "blank" string ==''.

Zachary
+26  A: 

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 == ''
Gumbo
I would add: If the compiler, or the interpreter, remember the operator precedence, the programmer shouldn't relay on this. If a fellow misinterpret the precedence order can introduce bugs difficult to maintain. My advice is to use the parentesis to solve any ambiguity in order to make your colleague confident on wath you write at the first glance.
Eineki
+4  A: 

&& has a higher operator precendence than || , so you are effectively saying:

if($country == 224 || $country == 223 || ($country == 39 && $zip == '' ))
Tom Haigh
+10  A: 

The problem is the order in which PHP checks your boolean operators. First it sees a condition, then an OR, and it thinks: Heck, yeah! The condition is met. Why should I bother read and execute the rest of this stuff?

Actually, this is a feature. Think of this constellation:

if (something_probable () OR something_very_expensive_to_compute ())

Then it is nice of PHP to not evaluate the second one, if the first one already passes the test.

Try using parentheses:

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

Cheers,

Boldewyn
thanks I never knew this and this seems like something very good to know!BTW that worked for my problem
jasondavis
You're welcome! Don't forget to accept any of the answers (Gumbo and Gortok are giving in principle the same answer. I don't know who was the first).
Boldewyn
+5  A: 

I like the first answer but anyway wouldn't more readable:

<?php

$need_zip_code = array(224, 222, 332, 222/* etc....*/);

if (in_array($country, $need_zip_code) &&  $zip === '') {
 // do your stuff....
}


?>
Gabriel Sosa