Thread: Simply If - Elseif Statements?

1. 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. 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. Originally Posted by StarLion
That can be simplified into 1 IF/ELSE.
Ok, but how?

4. 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. Originally Posted by StarLion
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. Originally Posted by RobAC
Why is the check for 2012 invalid?
look a little closer. what are you comparing 2012 to?

7. Originally Posted by iackay
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. Originally Posted by RobAC
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)

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

Posting Permissions

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