php流程控制
if语句,switch语句,while循环,do…while循环,for循环,foreach循环,break中断循环,continue指令.下面通过星期函数来演示这些.
程序三种流程控制结构
1、顺序结构
2、选择结构
3、循环结构
结果如下实例代码如下:
- $d=date("D");
- if ($d=="Tue")
- echo "今天是星期二";
- else
- echo "今天不是星期二";
- ?>
通过数组可以判断星期几.
执行结果应如下实例代码如下:
- $srttime=date("w",time());
- $array=array('天','一','二','三','四','五','六');
- $todaytime=date("Y年m月d日 星期{$array[$srttime]}",time());
- echo $todaytime;
- ?>
- 1. if (条件一) {
- ?⑹?/p>
- }
- <HTML>
- <HEAD>
- <TITLE> IF - 1 </TITLE>
- </HEAD>
- <BODY>
- <?
- $a = 100 ;
- if ( $a == 100 ) {
- echo "A is 100. ";
- }
- ?>
- </BODY>
- </HTML>
- 或
- if (条件一) {
- ?⑹鲆?/p>
- } else {
- ?⑹龆?/p>
- }
- <HTML>
- <HEAD>
- <TITLE> IF … ELSE - 1 </TITLE>
- </HEAD>
- <BODY>
- <?
- $a = 120 ;
- if ($a < 100 ) {
- echo " A was small than 100. ";
- }
- else {
- echo " A was big than 100. ";
- }
- ?>
- </BODY>
- </HTML>
- <HTML>
- <HEAD>
- <TITLE> IF … ELSE - 2 </TITLE>
- </HEAD>
- <BODY>
- <?
- $file = "files.txt" ;
- if ( file_exists($file) ) { //?z查?n案是否存在
- echo " ?是?n案 files.txt的?热? <BR> ";
- readfile ($file) ; //?出?n案?热?br /> }
- else {
- echo " ?o此?n案 <BR>";
- }
- ?>
- </BODY>
- </HTML>
- 或
- if (条件一) {
- ?⑹鲆?/p>
- } elseif {
- ?⑹龆?/p>
- } elseif {
- ?⑹鋈?/p>
- }
- ...........
- else {
- ?⑹?
- }
- <HTML>
- <HEAD>
- <TITLE> IF … ELSEIF … ELSE - 1 </TITLE>
- </HEAD>
- <BODY>
- <?
- $a = 100 ;
- $b = 200 ;
- if ($a > $b) {
- echo "a is bigger than b";
- } elseif ($a == $b) {
- echo "a is equal to b";
- } else {
- print "a is smaller than b";
- }
- ?>
- </BODY>
- </HTML>
- 2. while (条件一) { //条件?檎? ??
- ?⑹鲆?/p>
- }
- <HTML>
- <HEAD>
- <TITLE> WHILE </TITLE>
- </HEAD>
- <BODY>
- <?
- $a = 1 ;
- while ( $a < 10 ) {
- echo "$a <BR>";
- $a++;
- }
- ?>
- </BODY>
- </HTML>
- 3. do {
- ?⑹?/p>
- } while (条件); //条件?檎? ??
- <HTML>
- <HEAD>
- <TITLE> Do .. WHILE - 1 </TITLE>
- </HEAD>
- <BODY>
- <?
- $a = 1 ;
- do {
- echo "$a <BR>";
- $a++;
- } while ( $a < 10 )
- ?>
- </BODY>
- </HTML>
- 4. for (初始条件; 判?嗵跫? 条件改??⑹? {
- ?⑹?/p>
- }
- <HTML>
- <HEAD>
- <TITLE> For - 1 </TITLE>
- </HEAD>
- <BODY>
- <?
- for ( $a = 1 ; $a < 10 ; $a++ )
- {
- echo "$a <BR>";
- }
- ?>
- </BODY>
- </HTML>
- 5. break //中?嗾?诘霓?圈
- <HTML>
- <HEAD>
- <TITLE> BREAK </TITLE>
- </HEAD>
- <BODY>
- <?
- $abc[0] = '0' ;
- $abc[1] = '1' ;
- $abc[2] = '2' ;
- $a = 0 ;
- while ( $a < 4 )
- {
- if ( $abc[$a] == '2' ) { //若?檎? 跳?while()?圈
- break;
- }
- echo "$a";
- $a++;
- }
- ?>
- </BODY>
- </HTML>
- 6. continue //中?嗾?诘霓?圈, 跳到?圈????下一次
- <HTML>
- <HEAD>
- <TITLE> CONTINUE </TITLE>
- </HEAD>
- <BODY>
- <?
- for ($i=10 ; $i>1 ; $i--) {
- if ($i == 2 ) { //若?檎? ?束?次的for()?圈, ??下一次for()?圈
- continue;
- }
- echo "$i<br>";
- }
- ?>
- </BODY>
- </HTML>
- 7. switch (条件) {
- case 'case值1':
- ?⑹鲆?nbsp; //条件?case值相同,就?部分的?⑹? 直到遇到break才停止
- break;
- case 'case值2:
- ?⑹龆?br /> break;
- ............
- default:
- ?⑹?
- break;
- }
- <HTML>
- <HEAD>
- <TITLE> SWITCH </TITLE>
- </HEAD>
- <BODY>
- <?
- switch ($i) {
- case 0:
- echo "i equals 0";
- case 1:
- echo "i equals 1";
- case 2:
- echo "i equals 2";
- }
- ?>
- </BODY>
- </HTML>
- <HTML>
- <HEAD>
- <TITLE> Switch - SWITCH </TITLE>
- </HEAD>
- <BODY>
- <?
- switch ( $a ) {
- case '1':
- echo "one";
- break;
- case '2':
- echo "two";
- break;
- case '3':
- echo "three";
- break;
- case '4':
- echo "four";
- break;
- case '5':
- echo "five";
- break;
- default:
- echo "ZERO";
- break;
- }
- ?>
- </BODY>
- </HTML>
注意事项
PHP 语法中在每条指令结束时都要加上分号 ;,但是在部分结尾符号 } 后面不用加上分号结束.
在流程的部分分隔符号上,都是使用 { 当作部分的开头,用 } 当作结尾,和 C 语言相同.不过 C 可以定义 begin 当开头、end 当结尾 (像 Pascal),而 PHP 中不能做这种特殊的定义.