PHP control structure: Switch

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.