This article describes the use of switch statements 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: The code to is
executed if expression = Label1;
break;
Case LABEL2: The code to is
executed if expression = Label2;
break;
Default:
code to was 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 a switch to determine a range of values, or to customize a condition in a case.
<?php
Header ("Content-type:text/html;charset=utf8");
$score =50;
Switch ($score)
{case
$score >=90 && $score <=100:
echo "excellent <br>";
break;
Case $score >=80 && $score <90:
echo "Liang <br>";
break;
Case $score >=70 && $score <80:
echo "<br>";
break;
Case $score >=60 && $score <70:
echo "pass <br>";
break;
Case $score >=0 && $score <60:
echo "Less than lattice <br>";
break;
Default:
echo "score input error <br>";
>
Simple example
<?php//switch Details/Situation one: When the numerical match is automatically converted into a string $a = 1;
Switch ($a) {case "1": Echo ' hello1 ';
Break
Default:echo ' Sorry none is the same! ';
Break } Echo ' <br/> '. '
Successful exit ";
Echo '
Run results
Hello1
Successful Exit
Hello2
Successful Exit
Hello3
Successful Exit
Hello4
Successful Exit
Hello5
Successful Exit
Hello6
Successful Exit
Hello7
Successful Exit
hello81
Successful Exit
I'm exit 5th,
exit
hello61. Hello6, I'm number 2nd. Exit
successfully