简体   繁体   中英

PHP if Statement with OR

I seem to be having a mysterious problem with the use of OR or || in a PHP if statement. My code is this:

if ($region=='ibiza' || 'mallorca' || 'menorca' || 'andalucia' || 'basque' || 'cataluna' || 'centralspain' || 'greenspain' || 'pyrenees' || 'rioja' || 'valencia') {
    $GoTo = "/spain/".$region.".php";    
}

No matter what value I give to $region , $GoTo always comes out as /spain/$region.php ie the first if loop always evaluates as "true". There are other ways I could do this, but I don't see why this method doesn't work.

You want to use an or statement like this:

if ($region=='ibiza'||$region=='mallorca'||$region=='menorca'....)

But in your case, you might want to use in_array()

$locals=array(ibiza','mallorca','menorca','andalucia','basque','cataluna','centralspain','greenspain','pyrenees','rioja','valencia')
if (in_array($region, $locals)) {
    $goto="something...";
}

the answer @Fluffeh posted is correct. Edit: @Findus explanation is also correct.

if you want to be lazy you can do it like this:

if(in_array($region,array('ibiza','mallorca','menorca','...','..'))){}

this is because the statement 'mallorca' (and the ones following) evaluates to true. you should use a comparison, like $region == "mallorca", and similar for all the others.

You can simlify your code like this:

<?php
    $selectedRegions1 = array('ibiza','mallorca','menorca','andalucia','basque','cataluna', 'centralspain','greenspain','pyrenees','rioja','valencia');
    $selectedRegions2 = array('brittany','burgundy','alps','aquitaine','loire','languedoc', 'paris','provence');
    $selectedRegions3 = array('grancanaria','lapalma','lanzarote','tenerife');
    $selectedRegions4 = array('atlas','essaouira','fez','marrakech');
    if (in_array($region, $selectedRegions1 )) {
       $GoTo = "/spain/".$region.".php";
    }elseif(in_array($region,$selectedRegions2)) {
        $GoTo = "/france/".$region.".php";
    }elseif (in_array($region,$selectedRegions3)) {
        $GoTo = "/canaries/".$region.".php";
    }elseif (in_array($region,$selectedRegions4)) {
    $GoTo = "/morocco/".$region.".php";
    }
?>

The problem with your code was that you forgot '$region ==' after the '||':

if ($region=='ibiza'|| $region=='mallorca'|| $region=='menorca'|| $region=='andalucia'|| $region=='basque'|| $region=='cataluna'|| $region=='centralspain'|| $region=='greenspain'|| $region=='pyrenees'|| $region=='rioja'|| $region=='valencia')

You have write if statement like this

if ($region=='ibiza'|| $region=='mallorca'|| $region=='menorca'|| $region=='andalucia'|| $region=='basque'|| $region=='cataluna'|| $region=='centralspain'|| $region=='greenspain'|| $region=='pyrenees'|| $region=='rioja'||$region=='valencia') {
$GoTo = "/spain/".$region.".php";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM