www.webdeveloper.com
Results 1 to 8 of 8

Thread: PHP || (and) operator question

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    9

    Thumbs up PHP || (and) operator question

    I am trying to make my navigation to change color when selected. the class for selected is "current"

    so I wrote this:

    <ul class="<?php if(myfullpath() == "http://www.domain.com/first.php" || "http://www.domain.com/second.php"){echo "current";} else echo " "; ?>">

    so if the url in the address bar is not first.php or second.php it should return false and echo " ", but for some odd reason this is returning true and posting class="current"> even if the url is posting http://www.domain.com/fourth.php.

    JUST FYI here is the myfullpath function:

    function myfullpath() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }


    thanks for your help.

  2. #2
    Join Date
    Jan 2011
    Posts
    19
    Hi you missed a point there
    <ul class="<?php if(myfullpath() == "http://www.domain.com/first.php" || "http://www.domain.com/second.php"){echo "current";} else echo " "; ?>">
    check If statement
    It should be
    <ul class="<?php if(myfullpath() == "http://www.domain.com/first.php" || myfullpath() == "http://www.domain.com/second.php"){echo "current";} else echo " "; ?>">
    PS: I didnt execute this code... have to check yourself

  3. #3
    Join Date
    May 2009
    Posts
    9

    Thanks!

    Hi Thank you that's right I forgot about that, I atually ended up using elseif instead, so :

    if(myfullpath() == "http://www.domain.com/first.php"){echo "current";} elseif(myfullpath() == "http://www.domain.com/second.php") {
    echo "current";}else echo " ";
    I wonder which is a better practice or if there is any difference in server resources.

    Thanks again for all your help

  4. #4
    Join Date
    Jan 2011
    Posts
    19
    Both, in this case, are the same. As in using A || B it checks A and if true it ignores B. Similarly in ifelse if first IF statement is true it ignores the second.

    But from a view of code maintenance its a single if statement thats better.

  5. #5
    Join Date
    May 2009
    Posts
    9
    yea you are right the elseif statements can become hard to figure out after a while and also || is much shorter and easier to troubleshoot. I have changed it all over to || and it works great! thank you so much !

  6. #6
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    PHP Code:
    <?php
    $fullPath 
    myfullpath();
    $class="";
    if ( 
           (
    $fullPath == "http://www.domain.com/first.php") || 
           (
    $fullPath == "http://www.domain.com/second.php")
    )
    {
         
    $class 'class="current" ';
    }
    ?>

    <ul <?php echo $class?> >

    Just a couple of points to add:

    -- single call to the function (small improvement to performance, plus a bit easier to maintain/reuse later on)
    -- [expanding from adarshakb] wrap each IF check in it's own parenthesis, then wrap all the IF checks in parenthesis
    -- I prefer to to create the empty $class string, then populate it if needed, and echo out the entire string. This way, you don't leave unnecessary empty 'class=""' in your HTML (again, just a small personal preference)

  7. #7
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Just for clarification the || operator is the OR operator, not the AND operator which is &&. The thread title is a little confusing in that regard.

  8. #8
    Join Date
    Jan 2011
    Posts
    19
    YA!!! didnt notice that :P

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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