Home > Article > Backend Development > PHP process control_PHP tutorial
if statement, switch statement, while loop, do...while loop, for loop, foreach loop, break interrupt loop, continue instruction. These are demonstrated below using the day of the week function.
Three process control structures of the program
1. Sequential structure
2. Select structure
3. Loop structure
The results are as follows
The code is as follows | Copy code | ||||||||
$d=date("D");
echo "Today is Tuesday"; elseecho "Today is not Tuesday"; ?>
|
The code is as follows | Copy code |
$srttime=date("w",time()); $array=array('天','一','二','三','四','五','六'); $todaytime=date("Y year m month d day week {$array[$srttime]}",time()); echo $todaytime; ?> |
1. if (条件一) {
?⑹?/p>
}
或
if (条件一) {
?⑹鲆?/p>
} else {
?⑹龆?/p>
}
或
if (条件一) {
?⑹鲆?/p>
} elseif {
?⑹龆?/p>
} elseif {
?⑹鋈?/p>
}
...........
else {
?⑹?
}
} elseif ($a == $b) {
echo "a is equal to b";
} else {
print "a is smaller than b";
}
?>
2. while (条件一) { //条件?檎? ??
?⑹鲆?/p>
}
3. do {
?⑹?/p>
} while (条件); //条件?檎? ??
4. for (初始条件; 判?嗵跫? 条件改??⑹? {
?⑹?/p>
}
5. break //中?嗾?诘霓?圈
6. continue //中?嗾?诘霓?圈, 跳到?圈????下一次
case 'case value 1':
break;
case 'case value 2:
break;
… but
default:
?⑹?
break;
}
注意事项
PHP 语法中在每条指令结束时都要加上分号 ;,但是在部分结尾符号 } 后面不用加上分号结束。
在流程的部分分隔符号上,都是使用 { 当作部分的开头,用 } 当作结尾,和 C 语言相同。不过 C 可以定义 begin 当开头、end 当结尾 (像 Pascal),而 PHP 中不能做这种特殊的定义。