Home >Backend Development >PHP Tutorial >PHP flow control statement_PHP tutorial

PHP flow control statement_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:12:56780browse

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;
}
?>
switch ($i) {

case "apple":

            echo "i is apple";
 代码如下 复制代码

switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>

         break;

case "bar":
           echo "i is bar";

         break;
 代码如下 复制代码

switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>

case "cake":

            echo "i is cake";
         break;

}
 代码如下 复制代码
if(expression1){
statement1;
}else if(expression2){
statement2;
}

else{
statementn;
}
?> Example 2
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;<🎜> }<🎜> ?>
Example 3
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 if...else statement can only choose two results: either true or false. But what should I do now that there are more than 2 choices Woolen cloth? At this time, you can use the esleif (can also be written as else if) statement to execute. The syntax format is:
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
 代码如下 复制代码


$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!";
?>


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!";
?>

http://www.bkjia.com/PHPjc/629271.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/629271.htmlTechArticleThis article will talk about two process judgment statements in PHP, including switch statement and if else, if() statement. Let’s take a look at an example in detail below. The code is as follows Copy code switch(variable){ ca...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn