0

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.

5 Answers 5

4

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','...','..'))){}
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, exactly what I was writing after my initial post :)
@MattHumphrey yeah but to be honest the main reason I do it: because im lazy
i'm a selfthought programmer, but these questions always make me wonder did you even read a tutorial/blog/item/thing on the subject, in this case if statements.... because this stuff is easy to find on google.
I'll opt for this rather than Fluffeh's solution, just because it seems more readily understandable when I come back to it later. Thanks to all for the assistance: 5 answers in 8 minutes. Incredible!
Yes, user338128, I had done quite a lot of searching before coming here. I didn't see anything that said that my shortcut use of || wouldn't work, and I thought I had seen it elsewhere being used. With hindsight, obviously not.
4

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

Comments

1

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.

Comments

1

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')

1 Comment

I didn't forget '$region=='; I just thought I didn't need it. Anyway, the code is now modified and the page is up and working so thanks again to all who took the trouble to reply.
0

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

1 Comment

Thanks Harry (and Findus). I thought I could reduce the length of the if statement by my method, but obviously not! At least I now have it confirmed that I just cannot do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.