Home > Backend Development > PHP Tutorial > 笨鸟学php(4) 流程控制

笨鸟学php(4) 流程控制

WBOY
Release: 2016-06-13 12:02:52
Original
917 people have browsed it

笨鸟学php(四) 流程控制

一、选择结构

1. 单路分支

<?php if(条件){		执行一条语句;	}?>
Copy after login

2. 双路分支

<?php if(条件)	  	执行一条语句;	}else	  	执行一条语句;	}?>
Copy after login

3. 多路分支

<?php if(条件){	   	   执行一条语句; 	   }elseif(条件){ 		   执行一条语句; 	   }elseif(条件){ 		   执行一条语句; 	   }else if(条件){ 		   执行一条语句; 	   }else{ 		   执行一条语句; 	   }?>
Copy after login

<?php switch(变量){ 		case 值1: 			代码; 			break; 		case 值2: 			代码; 			break; 		default: 			代码; 	   }?>
Copy after login

4. 嵌套分支

<?php if(条件1){ 			if(条件2){ 				代码片段; 			}else{  				if(条件3){  					代码片段;  				}  			}  	    }else{  			if(条件4){  				代码片段;  			}else{  				swicth(){  					  				}  			}?>
Copy after login


二、循环结构

1. while循环

<?php $num=0; 	while($num < 100){		echo "这是执行第 {$num} 次输出的结果<br>";			$num++;	}?>
Copy after login

2. do-while循环

<?php $i = 0;	do {		echo "$i : this is do while <br>";		$i++;	} while ($i 
Copy after login

3. for循环

<?php for ($i = 0; $i < 10; $i++) {		echo "这是第 $i 次循环执行的结果<br>";	}?>
Copy after login
几个和循环有关的语句:

break;   continue;  exit;  return;


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template