简体   繁体   English

PHP if语句或

[英]PHP if Statement with OR

I seem to be having a mysterious problem with the use of OR or || 使用OR||似乎有一个神秘的问题 in a PHP if statement. 在PHP if语句中。 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". 无论我给$region赋予什么值, $GoTo总是以/spain/$region.php即第一个if循环的/spain/$region.php为“ 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: 您想要使用or语句,如下所示:

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

But in your case, you might want to use in_array() 但是在您的情况下,您可能要使用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. @Fluffeh发布的答案是正确的。 Edit: @Findus explanation is also correct. 编辑:@Findus的解释也是正确的。

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. 这是因为语句“ mallorca”(及其后的语句)的计算结果为true。 you should use a comparison, like $region == "mallorca", and similar for all the others. 您应该使用一个比较,例如$ region ==“ mallorca”,所有其他类似。

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 '||': 您的代码的问题是您在'||'之后忘记了'$ region ==':

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语句

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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