Simply If - Elseif Statements?

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

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;
     }

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 :stuck_out_tongue:

Ok, but how?

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?

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?

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)

So is this better?

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;
     }

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.)

Like this?

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;
     }

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.