There are three types of cycles:
1. For loop
Format:
For (initialization of the loop variable; loop condition; increment of the loop variable) {loop body}
2. While loop
Format:
while (condition) { loop body }
3. Do While loop
Format:
Do { loop body } while (condition);
Note: The semicolon after the while statement
Attention:
1), for loop for a regular cycle, with more
2), while and does while are different:
The while loop may not execute at one time, while the Do While loop executes at least once
Example:
<?PHP/** loop out a table, using for loop, while loop, do While loop, respectively*/ //For loop//Output table header Echo"<table width= ' 80% ' align= ' center ' border=1 cellpadding=5 clellspacing=0> '; //output of the outer loop control row for($i= 1;$i<=5;$i++) { //Interlaced color Change if($i% 2 ==0){ Echo("<tr align= ' center ' bgcolor= ' green ' >"); }Else{ Echo("<tr align= ' center ' >"); } //the output of the Inner loop control column for($j= 1;$j<=8;$j++) { Echo"<td>".$i*$j." </td> "; } Echo"</tr>"; } Echo"</table>"; //While Loop Echo"<table width= ' 80% ' align= ' center ' border=1 cellpadding=5 clellspacing=0> '; $m= 1; while($m<= 5) { //Interlaced color Change if($m% 2 ==0){ Echo("<tr align= ' center ' bgcolor= ' green ' >"); }Else{ Echo("<tr align= ' center ' >"); } //the output of the Inner loop control column $n= 1; while($n<= 8) { Echo"<td>".$m*$n." </td> "; $n++; } Echo"</tr>"; $m++; } //Do and Loop Echo"<table width= ' 80% ' align= ' center ' border=1 cellpadding=5 clellspacing=0> '; $k= 1; Do{ //Interlaced color Change if($k% 2 ==0){ Echo("<tr align= ' center ' bgcolor= ' green ' >"); }Else{ Echo("<tr align= ' center ' >"); } //the output of the Inner loop control column $p= 1; Do { Echo"<td>".$k*$p." </td> "; $p++; } while($p<= 8); Echo"</tr>"; $k++; } while($k<= 5);?>
PHP Note III: Use of loops (loop output table)