Php switch statement usage

Source: Internet
Author: User
Tags php switch
Switch statements are also the most common logical control statements in object-oriented programming. The Switch statement is used to execute different actions based on multiple different conditions-that is, when the conditions are different, different logical operations are executed.

Switch statements are also the most common logical control statements in object-oriented programming. The Switch statement is used to execute different actions based on multiple different conditions-that is, when the conditions are different, different logical operations are executed.

This article describes the usage of the switch statement in php, which is similar to the switch usage in other statements, but note that there is a break statement.

Standard syntax for switch statements in PHP:

Switch (expression) {case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2 ;}

Example:

Switch ($ I) {case 1: echo 1; break; case 2: echo 2; break; default: echo 'others ';}

You can also use switch to determine a value range, or define a condition in case.

<? Phpheader ("content-type: text/html; charset = utf8"); $ score = 50; switch ($ score) {case $ score> = 90 & $ score <= 100: echo "Excellent
"; Break; case $ score >=80 & $ score <90: echo" Good
"; Break; case $ score >=70 & $ score <80: echo" Medium
"; Break; case $ score >=60 & $ score <70: echo" Pass
"; Break; case $ score >=0 & $ score <60: echo" fail
"; Break; default: echo" Incorrect Score Input
";}?>

Simple Example

<? Php // switch details // case 1: The value is automatically converted to the string $ a = 1 when matching values; switch ($ a) {case "1": echo 'hello1 '; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo'

'; // Case 2: The value is automatically converted to the string $ a = 1 when it is matched. switch ($ a) {case '1': echo 'hello2'; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 3: It is automatically converted to a value $ a = '1' when matching characters; switch ($ a) {case 1: echo 'hello3'; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 4: The value $ a = "1"; switch ($ a) {case 1: echo 'hello4'; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 5: float can also match $ a = 1.1; switch ($ a) {case 1.1: echo 'hello5'; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 6: If not 0 is true $ a = true; switch ($ a) {case 1: echo 'hello6'; break; case true: echo 'hello61 '; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 7: matching boolean $ a = true; switch ($ a) {case true: echo 'hello7'; break; case 2: echo 'hello71 '; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 8: matching null $ a = null; switch ($ a) {case 2: // ''" "false 0 can access echo 'hello8 '; break; case null: echo 'hello81 '; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 9: exit sequence $ a = 5; switch ($ a) {case 1: echo 'hello6'; break; case 2: echo 'exit 2 '; break; case 5: echo 'exit 5'; // break; case true: echo 'hello61'; break; default: echo 'Sorry none is the same! '; Break;} echo'
'.' Exited successfully · '; echo' '; // Case 10: exit sequence $ a = 50; switch ($ a) {default: echo 'Sorry none is the same! '; // Break; case 50: echo 'hello6'; // break; case 2: echo 'exit 2'; break; case 5: echo 'exit 5 '; // break; case 6: echo 'hello61'; break;} echo'
'.' Exited successfully · '; echo' ';?>

Running result

Hello1 successfully exits · hello2 successfully exits · hello3 successfully exits · hello4 successfully exits · hello5 successfully exits · hello6 successfully exits ·· · hello7 successfully exited · hello81 successfully exited · I exited at Exit 5 hello61 successfully · hello6 I successfully exited at Exit 2 ····

,

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.