Home >Backend Development >PHP Tutorial >PHP flow control statement_PHP tutorial
This article will talk about two kinds of process judgment statements in PHP, including switch statements and if else, if() statements. Let’s take a look at examples in detail.
代码如下 | 复制代码 |
switch(variable){ case value1: statement1; break; case value2: … default: defulat statement; } |
The switch statement compares the value of the variable with the value in the case in turn. If they are not equal, continue to search for the next
case; if equal, execute the corresponding statement until the switch statement ends or break is encountered.
The code is as follows | Copy code |
代码如下 | 复制代码 |
switch ($i) { case "apple": echo "i is apple"; break; case "bar": echo "i is bar"; break; case "cake": echo "i is cake"; break; } ?> |
case "apple":
echo "i is apple";代码如下 | 复制代码 |
switch ($i) { |
case "bar":
echo "i is bar";
代码如下 | 复制代码 |
switch ($i) { |
echo "i is cake";
break;
代码如下 | 复制代码 |
if(expression1){ statement1; }else if(expression2){ statement2; } … else{ statementn; } |
The code is as follows | Copy code |
switch ($i) {<🎜> case 0:<🎜> echo "i equals 0";<🎜> break;<🎜> Case 1:<🎜> echo "i equals 1";<🎜> break;<🎜> Case 2:<🎜> echo "i equals 2";<🎜> break;<🎜> }<🎜> ?> |
The code is as follows | Copy code |
switch ($i) {<🎜> case 0:<🎜> case 1:<🎜> case 2:<🎜> echo "i is less than 3 but not negative";<🎜> Break;<🎜> case 3:<🎜> echo "i is 3";<🎜> }<🎜> ?> |
The code is as follows | Copy code |
if(expression1){ statement1; }else if(expression2){ statement2; } … else{ statementn; } |
Example
代码如下 | 复制代码 |
$moth = date(“n”); $today = date(“j”); if($today >= 1and $today <= 10){ echo’今天是’.$moth.’月’.$today.’日上旬’; }elseif ($today >10 and $today <=20){ echo’今天是’.$moth.’月’.$today.’日中旬’; }else{ echo’今天是’.$moth.’月’.$today.’日下旬’; } ?> |
If the current date is Friday, the following example will output "Have a nice weekend!", if it is Sunday, it will output "Have a nice Sunday!", otherwise it will output "Have a nice day!":
The code is as follows
|
Copy code | ||||
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>