SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply If - Elseif Statements?

    Is there a way to simplify the following series of if / elseif statements, or are they correct as written?

    Code PHP:
    if (($results[0][leather_stitch]  == "Red") && ($results[0][model_year] == "2011" || "2012"))
             {
             $leather_stitch = 395;
             }
        elseif (($results[0][leather_stitch]  == "Blue") && ($results[0][model_year] == "2011" || "2012"))
             {
             $leather_stitch = 395;
             }
        elseif (($results[0][leather_stitch]  == "Yellow") && ($results[0][model_year] == "2011" || "2012"))
             {
             $leather_stitch = 395;
             }
        else {
            $leather_stitch = 0;
         }

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    That can be simplified into 1 IF/ELSE.

    That however sounds like a homework problem, so I'm not going to do it for you :P

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    That can be simplified into 1 IF/ELSE.
    Ok, but how?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Hint: Your outcome values are all the same, so it doesnt matter if the car is Red, Blue, or Yellow, as long as it is one of the three.

    PS: As written, this code is a syntax error. (The check for Model year 2012 is invalid)

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Hint: Your outcome values are all the same, so it doesnt matter if the car is Red, Blue, or Yellow, as long as it is one of the three.

    PS: As written, this code is a syntax error. (The check for Model year 2012 is invalid)
    Why is the check for 2012 invalid?

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RobAC View Post
    Why is the check for 2012 invalid?
    look a little closer. what are you comparing 2012 to?

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iackay View Post
    look a little closer. what are you comparing 2012 to?
    I'm basically stating if model_year= 2011 OR 2012...so I don't see how I'm comparing 2012 to anything?

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RobAC View Post
    Why is the check for 2012 invalid?
    As iackay said.
    PHP has no memory between clauses. A || B means that A must be a logical test, and B must be a logical test. Your B is not a logical test; it's a string. (Which will always evaluate to TRUE if said string is not Null)
    Last edited by StarLion; Aug 9, 2011 at 10:35. Reason: fixed name. sorry iackay!

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So is this better?
    Code PHP:
    if (($results[0][leather_stitch]  == "Red")||($results[0][leather_stitch]  == "Yellow")||($results[0][leather_stitch]  == "Blue") && ($results[0][model_year] == "2011") || ($results[0][model_year] == "2012")))
             {
             $leather_stitch = 395;
             }
        else {
            $leather_stitch = 0;
         }

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Almost perfect.
    You've got the simplification down. Now group your model_year clauses (wrap them in parenthesis)

    I'm going to use letters to make it clear why:

    A || B || C && D || E

    Parse left to right. Here's how the parser logics it out: (Ans = previous line's answer.)

    A || B. Fine.
    Ans || C. Fine. Now we've got 'Is it red, blue, or yellow'.
    Ans && D. Now you've got "Is it red, blue, or yellow, and model year 2011".
    Ans || E. Er... now you're asking "(Is it red, blue, or yellow and model year 2011) OR ( is it model year 2012). Not what you were looking for, because this will return true if the model year is 2012, regardless of color.

    Personally, if i were coding it, i'd group it as:
    (A || B || C) && (D || E)

    (the grouping around A/B/C is not strictly necessary, but i do it anyway for my sanity.)

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    home
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this?

    PHP Code:
    if (($results[0][leather_stitch]  == "Red" || $results[0][leather_stitch]  == "Yellow" || $results[0][leather_stitch]  == "Blue") && ($results[0][model_year] == "2011" || $results[0][model_year] == "2012"))
             {
             
    $leather_stitch 395;
             }
        else {
            
    $leather_stitch 0;
         } 

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Only thing i can see is that you need to quote your array keys ($results[0]['leather_stitch']), but other than that it looks good to me.


Bookmarks

Posting Permissions

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