Paul
Paul

Reputation: 11746

Why is this PHP comparison failing?

I'm trying to compare a defined code against a set of blocked country codes. In the following example the country code is getting caught in my if block:

$country_code = 'US';

if ($country_code == ("NG" || "RO" || "VN" || "GH" || "SN" || "TN" || "IN" || "ID" || "KE" || "CN" || "CI" || "ZA" || "DZ" || "RU")) {
    print "COUNTRY CODE: $country_code<br>";
}

I see this for my results"

COUNTRY CODE: US

I wouldn't expect 'US' to get caught...what am I missing?

Upvotes: 1

Views: 211

Answers (6)

Michael Berkowski
Michael Berkowski

Reputation: 270617

You cannot chain together || in this way and get the result you expect. It will aways return TRUE since any non-empty string evaluates to true. Since the operand on the left of == is compared to the entire operand on the right, You are in effect saying:

if ($country_code == (TRUE ||TRUE||TRUE||TRUE||TRUE...);

While it would be valid to do something like the following, it gets out of hand:

if ($country_code == "N" || $country_code == "RO" || $country_code == "VN" ...)

Instead, use in_array();

$countries = array("NG","RO","VN","GH",...);
if (in_array($country_code, $countries) {
  print "COUNTRY CODE: $country_code<br>";
}

Upvotes: 6

Mark Tomlin
Mark Tomlin

Reputation: 8933

$country_code = 'US';

Here your setting the $country_code.

if ($country_code == ("NG" || "RO" || "VN" || "GH" || "SN" || "TN" || "IN" || "ID" || "KE" || "CN" || "CI" || "ZA" || "DZ" || "RU")) {

Here your evaluating $country_code against nothing really, but because it's all non-empty strings they to evaluate to TRUE, and because $country_code is a non-empty string, it also evaluates to TRUE.

    print "COUNTRY CODE: $country_code<br>";

Here your printing $country_code that you set in your first line.

}

What you want is something like this

if (
    $country_code == "NG" ||
    $country_code == "RO" ||
    $country_code == "VN" ||
    $country_code == "GH" ||
    $country_code == "SN" ||
    $country_code == "TN" ||
    $country_code == "IN" ||
    $country_code == "ID" ||
    $country_code == "KE" ||
    $country_code == "CN" ||
    $country_code == "CI" ||
    $country_code == "ZA" ||
    $country_code == "DZ" ||
    $country_code == "RU"
) {

Upvotes: 0

Starx
Starx

Reputation: 78991

This would be a lot simpler if done this way.

$codes = array("NG", "RO", "VN");

$search = array_search("NG");
if($search) {
    echo $codes[$search];
}

Upvotes: 0

Jim
Jim

Reputation: 22656

This:

("NG" || "RO" || "VN" || "GH" || "SN" || "TN" || "IN" || "ID" || "KE" || "CN" || "CI" || "ZA" || "DZ" || "RU")

is actually a boolean test which says "If any of these evaluate to true then true". Since non empty strings are true then this is true. Similarly "US" will be treated as true. So simplifying the statement we get: if(true == true).

Try using an array and the in_array function instead.

Upvotes: 3

mikelbring
mikelbring

Reputation: 1492

I would do this

$country_code = 'US';

if (in_array($country_code, array("NG", "RO", "VN", "GH", "SN", "TN", "IN", "ID", "KE", "CN", "CI", "ZA", "DZ", "RU")) {
    print "COUNTRY CODE: $country_code<br>";
}

Upvotes: 5

Brad
Brad

Reputation: 163240

What you are doing is OR-ing strings together. Since non-empty strings converted to boolean values are true, this evaluates to the following:

$country_code == true

Since $country_code is also a non-empty string, that also evaluates to true:

true == true

Thus, you get TRUE.

To solve your problem, you need to do what Pekka suggests:

if (in_array($country_code, array('NG', 'RO', etc.)))

See also:

Upvotes: 10

Related Questions