Negative condition if + word in URL

Discussion in 'PHP' started by Andres_BBB, Sep 7, 2012.

  1. #1
    Hi.

    We have this:

    <?php
    $uri = $_SERVER['REQUEST_URI'];
    if ( strpos($uri,'members') !== false ) {
    echo '<link href="cssfile-a.css" rel="stylesheet" type="text/css" />';
    } else {
    echo '<link href="cssfile-b.css" rel="stylesheet" type="text/css" />';
    }
    ?>

    Which makes the browser insert the cssfile-a file if the word "members" is in the url string.

    In other case, cssfile-b.css.

    The question is that I would like it in the negative way:

    if the word "members" is in the url string, then DO NOT insert the cssfile.a file, and INSERT IT in all the other cases.

    Is this possible?

    I have set it like this:

    <?php
    $uri = $_SERVER['REQUEST_URI'];
    if ( strpos($uri,'members') !== false ) {
    else {
    echo '';
    }
    }
    else {
    echo '<link href="cssfile-a.css" rel="stylesheet" type="text/css" />';
    }
    ?>

    It works. But I think that this is not valid PHP. I mean the correct way to do it.

    Thanks very much for your help.
     
    Andres_BBB, Sep 7, 2012 IP
  2. Triggs

    Triggs Active Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    You mean like this?

    
    <?php
    $uri = $_SERVER['REQUEST_URI'];
    if ( strpos($uri,'members') !== false ) echo '<link href="cssfile-a.css" rel="stylesheet" type="text/css" />'; 
    ?>
    
    PHP:
    Sorry, I'm a bit confused by your question.
     
    Triggs, Sep 7, 2012 IP
    itsme likes this.
  3. shubhamm

    shubhamm Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    I think is not equal is !=

    or you can try this..

    <?php
    $uri = $_SERVER['REQUEST_URI'];
    if(!strpos($uri,'members')){
    
    }else{
    
    echo '<link href="cssfile-a.css" rel="stylesheet" type="text/css" />';
    }
    
    ?>
    Code (markup):
     
    Last edited: Sep 8, 2012
    shubhamm, Sep 8, 2012 IP
  4. greyinfotech

    greyinfotech Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think it is a single line solution


    echo strpos($_SERVER['REQUEST_URI'],'members') ? '' : '<link href="cssfile-a.css" rel="stylesheet" type="text/css" />';
    Code (markup):
     
    greyinfotech, Sep 18, 2012 IP
  5. kbduvall

    kbduvall Peon

    Messages:
    71
    Likes Received:
    3
    Best Answers:
    4
    Trophy Points:
    0
    #5
    strpos returns the index of the found string. Therefore != isn't safe if the string is found at index 0 (the beginning of the string). You need to use the strict comparison with this function.

    Change your original code to this:
    
    <?php
    $uri = $_SERVER['REQUEST_URI'];
    if ( strpos($uri,'members') === false ) {
        echo '<link href="cssfile-a.css" rel="stylesheet" type="text/css" />';
    }
    ?>
    
    PHP:
     
    kbduvall, Sep 18, 2012 IP