#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    If...Else Statement - Simple yet so complex


    Hi,

    Here is the scenario - I want to show different widgets on a page based on page type.

    If page type is "blue", show widget A.
    else if page type is "red", show widget A,B,C,D
    else for any/all other pages, show widget BCD

    PHP Code:
    if($page == 'red'):
        echo 
    'widget A code';
    elseif(
    $page == 'blue'):
        echo 
    'widget A code' 'widget B/C/D code';
    else:
        echo 
    'widget B code' 'widget C code' 'widget D code';
    endif; 
    My problem with the above code is that I have to repeat the widget code in the second elseif condition where we check for page "blue".

    Is there a method by which:
    • If the page is red we execute widget A and then skip the rest of if...else statements.
    • If page is blue we execute widget A which we've already written for page red and rest of the widgets B,C & D which we've written for all the other pages.
    • If any other page apart from red and blue, show widgets B,C & D.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    There's no overlap in these conditions. If, for instance, you wanted to do:

    red: C,D
    Blue: B,C,D
    Green: A,B,C,D

    You could use the "fall-through" behavior of the SWITCH statement to accomplish this.

    However, you have no pattern here, it's just a fixed set of rules with no fall through or overlap. What you have is the most efficient (though I'd like to note that what you have doesn't match your prose descriptions, I assume due to a typo)

    Comments on this post

    • requinix agrees : milliseconds!
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    What you're asking about is how to get tricky with code. If "widget X code" is a bunch of code then it might be worthwhile, but at that point you should move it into a separate file or function and use that instead. I strongly suggest that: it's got more advantages than simply requiring less code.

    For the record the trickiness goes like this:
    1. Widget A always comes first and appears for red and blue pages.
    2. Widget B always comes after widget A (if used) and is shown for non-red pages.
    3. Widget C and D go together, appear after widget A and/or B (if used), and are shown for non-red pages.
    At that point you should realize 2 and 3 go together. So now it's
    PHP Code:
    if ($page == "red") {
        
    // widget A
    } else if ($page == "blue") {
        
    // widgets A, B, C, D
    } else {
        
    // widgets B, C, D

    and from there it should be more obvious that you can
    PHP Code:
    if ($page == "red" || $page == "blue") {
        
    // widget A
    }
    if (
    $page != "red") {
        
    // widgets B, C, D

    Comments on this post

    • NotionCommotion agrees : Always seeing a different side of things!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Sounds like you are not concerned with negligible speed, but not duplicating code, and having to maintain the differences. Do the various widget codes use common variables? If not, just put them in a function. If so, either pass them to each function, or use private properties to pass them to one another as shown below.
    PHP Code:
    if($page == 'red'):
        
    $this->widgetAcode();
    elseif(
    $page == 'blue'):
        
    $this->widgetAcode();
        
    $this->widgetBCDcode();
    else:
        
    $this->widgetBCDcode();
    endif; 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0
    That was pretty quick. I have already thought of the solutions suggested above:
    1) switch statement with conditional break; statement
    2) writing a function or putting the code in a separate file

    I was hoping that there is some other method in the if..else clause itself or a better logic that can accomplish this, but after hearing it from the experts, I feel confident that I'm not writing bad code for this particular scenario.

    Thank you all.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Sometimes the switch statement makes sense:

    PHP Code:
    switch(true)
    {
       case 
    $a['red']==1:doSomeThing();break;
       case 
    $a['blue']==1:doSomeElse();break;
       case 
    $a['green']==1:doSomeEvenElse();break;

  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Sometimes the switch statement works; I'd question whether or not using it like that makes sense though
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Originally Posted by E-Oreo
    Sometimes the switch statement works; I'd question whether or not using it like that makes sense though
    Agreed. I don't understand the fascination with switch(true). That's just If/Else in different clothing. It's less clear and takes longer to type.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Oooo, switch(true) is so very shinny!

    I fully agree its use for this example doesn't make sense, however, I didn't know the OP's actual applications and thought I would through it out there. There are actual examples, however, when it seems more intuitive to me than using a bunch of IF statements. You think there is ever a good time to do so?

IMN logo majestic logo threadwatch logo seochat tools logo