Jump to content

switch() is switching my head around


cssfreakie

Recommended Posts

Hi all i provided a little code  to someone in the misc thread to block users based on the user agent language as an alternative to ipaddresses. I used switch for this but the each line is executed even if my language code is different. Anyone have an idea why the switch code below is messing with me?

<?php
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ){

                $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
	// $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
	// need to remove spaces from strings to avoid error
	$languages = str_replace( ' ', '', $languages );
	$languages = explode( ",", $languages );
                // do something when you don't like the language
                switch ($languages[0]) { // first array item
                    case 'ko': // korea
                        echo "do something or set a variable";
                        break;
                    case 'ru-md'||'ru': // russia
                        echo "do something or set a variable";
                        break;
                    case 'zh-cn'||'zh-hk'||'zh-mo'||'zh-sg'||'zh-tw'||'zh': // china
                        echo "do something or set a variable";
                        break;
                    case 'ne': // india
                        echo "do something or set a variable";
                        break;
                    default:
                       echo "good";                         
                }
}
?>

I changed it to a different code that uses in_array() which works, but i really would love to know why the above is not working as intended, i looked at php.net, but didn't found something nice in the comments. Maybe a guru out here that knows?

<?php
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ){

                $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
	// $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
	// need to remove spaces from strings to avoid error
	$languages = str_replace( ' ', '', $languages );
	$languages = explode( ",", $languages );
                // do something when you don't like the language           
                $lang_array = array('ko','ru-md','ru','zh-cn','zh-hk','zh-mo','zh-sg','zh-tw','zh','ne');
                
                if (in_array($languages[0], $lang_array)){
                    echo 'bad stuff';
                }else{
                    echo 'good stuff';
                }
}

?>

 

Thanks guys!!  ;D

Link to comment
https://forums.phpfreaks.com/topic/229985-switch-is-switching-my-head-around/
Share on other sites

I test that here and mine comes back en-us which would fall under default. I would echo out the $languages[0] just before your case to make sure it's the value you're expecting.

It also might have to do with the

case 'zh-cn'||'zh-hk'||'zh-mo'||'zh-sg'||'zh-tw'||'zh': // china

if I'm matching multiple I usually do something like this.

 

case 'zh-cn':
case 'zh-hk':
case 'zh-mo':
case 'zh-sg':
case 'zh-tw':
case 'zh':

You can't put an OR condition is a case statement:

case 'ru-md'||'ru': // russia

 

You need a separate case statement for each value, but you don't have to repeat the code to be processed

 

Correct syntax

switch ($languages[0])
{
    case 'ko': // korea
       echo "do something or set a variable";
       break;
    case 'ru-md': // russia
    case 'ru':
       echo "do something or set a variable";
       break;
    case 'zh-cn': // china
    case 'zh-hk':
    case 'zh-mo':
    case 'zh-sg':
    case 'zh-tw':
    case 'zh':
       echo "do something or set a variable";
       break;
    case 'ne': // india
       echo "do something or set a variable";
       break;
    default:
       echo "good";                         
}

Thanks guys, where would we be without peeps like you ::)

 

I in fact echoed  $languages[0] on my system and it was as expected. but left it out here :)

 

Good to know that or is not possible. I think i'll go for the in_array() one seems to be less code ::)

But i am very happy you guys explained it, I am still a big noob in php  8)

Cheers!

 

csssfreakie

 

-edit: holy crap i used 4 smilies

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.