Onceyougettothepointwhereyouhave & nbsp; longerif-elseif-elseconditionals, you & nbsp; mayfindthatyoucansaveprogramming & nbsp; timeandclarifyyourcod
Once you get to the point where you have? Longer if-elseif-else conditionals, you? May find that you can save programming? Time and clarify your code by using? Switch conditional instead. The switch? Conditional takes only one possible? Condition, normally just a variable:
switch ($var) { case value1: statement(s)1; break; case value2: statement(s)2; break; default: statement(s)3; break;}
You must understand how a switch? Conditional works in order to use it? Properly. After the keyword switch,? Variable is identified within parentheses. PHP will then look at each case in order, trying to identify a matching value. Note? That, as with any other use of strings and? Numbers in PHP, numeric values wowould? Not be quoted; string values shocould be. After the case value section, a colon (not
A semicolon) prefaces the associated? Statements, which are normally indented? Beginning on the following line.
Once PHP finds a case that matches? The value of the conditional variable, it? Executes the subsequent statements. Here's the tricky part: once PHP has found? A matching case, it will continue going? Through the switch until it either comes? To the end of the switch conditional (? Closing curly bracket) or hits a break? Statement, at which point it exits the switch? Construct. Thus, it's imperative that you? Close every case-even the default case, for consistency's sake-with a break. This previous switch conditional is like? A rewrite:
if ($var == value1) { statement(s)1;} elseif ($variable == value2) { statement(s)2;} else { statement(s)3;}
Because the switch conditional uses? The value of $ var as its condition, it first? Checks to see if $ var is equal to value1? And, if so, executes statement (s) 1. If not, it checks to see if $ var is equal to value2? And, if so, executes statement (s) 2. If? Neither condition is met, the default action? Of the switch conditional is to execute? Statement (s) 3 .? With this in mind, let's rewrite the colors? Conditional as a switch.