www.webdeveloper.com
Results 1 to 11 of 11

Thread: if switch

  1. #1
    Join Date
    Sep 2003
    Location
    Omaha, Nebraska, USA
    Posts
    55

    if switch

    Here's a sample of the code that I'm using to detect my user's browser.
    PHP Code:
    if (stripos($_SERVER['HTTP_USER_AGENT'], 'chrome')) {
        echo 
    'chrome';
    } elseif (
    stripos($_SERVER['HTTP_USER_AGENT'], 'firefox')) {
        echo 
    'firefox';
    } elseif (
    stripos($_SERVER['HTTP_USER_AGENT'], 'msie')) {
        echo 
    'internet explorer';
    } elseif (
    stripos($_SERVER['HTTP_USER_AGENT'], 'presto')) {
        echo 
    'opera';
    } elseif (
    stripos($_SERVER['HTTP_USER_AGENT'], 'safari')) {
        echo 
    'safari';
    } else {
        echo 
    'unknown browser';

    At this stage of it's development, it works just fine. The problem is that I'd rather write it as a switch, but have failed.
    PHP Code:
    if (stripos($_SERVER['HTTP_USER_AGENT'], $i)) {
        case 
    'chrome':
            echo 
    'chrome';
            break;
        case 
    'firefox':
            echo 
    'firefox';
            break;
        case 
    'msie':
            echo 
    'internet explorer';
            break;
        case 
    'opera':
            echo 
    'opera';
            break;
        case 
    'safari':
            echo 
    'safari';
            break;
        default:
            echo 
    'unknown';

    Any ideas where my problem is? Thanks!
    Have You Played Atari Today?

  2. #2
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    You have nothing set to check against in your example
    _,-O
    O
    (_)) ubuntu -
    -`-O

  3. #3
    Join Date
    Jun 2009
    Location
    NY
    Posts
    96
    First of all,
    This is wrong
    if (stripos($_SERVER['HTTP_USER_AGENT'], $i)) {
    case 'chrome':
    echo 'chrome';
    break;
    ...

    Should use switch statement

    I am not sure what $i is. But in any case, switch statement is not too useful in here. Think about it, in the switch statement, you are merely checking if the match exists. The function will return non FALSE value if it does. What you need though is an actual value found in the user agent string. Nested if's are fine, but if you must shorten the code, try this:

    PHP Code:
    $browsers = array('firefox''opera''chrome''msie''presto''safari'); 
    foreach (
    $browsers as $browser
    {    
        if (
    stripos($_SERVER['HTTP_USER_AGENT'], $browser) !== FALSE)    
        {        
            echo 
    $browser;        
            break; 
    // Required, some browsers may have multiple agent names    
        



  4. #4
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    Above will return Chrome AND Safari on Chrome browsers BTW.
    _,-O
    O
    (_)) ubuntu -
    -`-O

  5. #5
    Join Date
    Jun 2009
    Location
    NY
    Posts
    96
    No it won't. See the break statement in the foreach.

  6. #6
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    Gotcha
    _,-O
    O
    (_)) ubuntu -
    -`-O

  7. #7
    Join Date
    Jun 2009
    Location
    NY
    Posts
    96
    Important thing to note though, is that chrome should be placed before safari in the array for this to work. Likewise, opera should be placed before presto. Otherwise, chrome will display safari and opera will display presto.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,158
    Just because I was bored, here's a different approach.

    PS: I'm not claiming this is better, I just wanted to see if I could do it this way.
    PHP Code:
    <?php
    // only need to change this array to change list of browsers
    $browsers = array(
       
    'chrome' => 'chrome',
       
    'firefox' => 'firefox',
       
    'msie' => 'internet explorer',
       
    'opera' => 'opera',
       
    'safari' => 'safari'
    );
    // rest of this should not have to change
    $regexp = array();
    foreach(
    $browsers as $key => $value) {
       
    $regexp['/^.*'.preg_quote($key).'.*$/i'] = $value;
    }
    $browser preg_replace(
       
    array_keys($regexp),
       
    $regexp,
       
    $_SERVER['HTTP_USER_AGENT'],
       
    1,
       
    $count
    );
    if(
    $count == 0) {
       
    $browser 'unknown';
    }
    echo 
    $browser;
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    ^ 1up
    _,-O
    O
    (_)) ubuntu -
    -`-O

  10. #10
    Join Date
    Sep 2003
    Location
    Omaha, Nebraska, USA
    Posts
    55
    Quote Originally Posted by WebWarrior View Post
    First of all,
    This is wrong
    if (stripos($_SERVER['HTTP_USER_AGENT'], $i)) {
    case 'chrome':
    echo 'chrome';
    break;
    ...

    Should use switch statement
    Ah...a typo...don't know what I was thinking.

    Quote Originally Posted by WebWarrior View Post
    I am not sure what $i is.
    I was just using the example variable used in the documentation. http://php.net/manual/en/control-structures.switch.php

    Quote Originally Posted by WebWarrior View Post
    But in any case, switch statement is not too useful in here. Think about it, in the switch statement, you are merely checking if the match exists. The function will return non FALSE value if it does. What you need though is an actual value found in the user agent string. Nested if's are fine, but if you must shorten the code, try this:

    PHP Code:
    $browsers = array('firefox''opera''chrome''msie''presto''safari'); 
    foreach (
    $browsers as $browser
    {    
        if (
    stripos($_SERVER['HTTP_USER_AGENT'], $browser) !== FALSE)    
        {        
            echo 
    $browser;        
            break; 
    // Required, some browsers may have multiple agent names    
        


    I like short code, thanks!
    Have You Played Atari Today?

  11. #11
    Join Date
    Sep 2003
    Location
    Omaha, Nebraska, USA
    Posts
    55
    Quote Originally Posted by NogDog View Post
    Just because I was bored, here's a different approach.
    Gosh I have such a hard time with regular expressions, that I shy away from them as much as I can.
    Have You Played Atari Today?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles