PHP基础学习第三讲(循环讲解)

来源:互联网 发布:提升排名软件搜狗优化 编辑:程序博客网 时间:2024/06/06 00:21

1、重要知识点

第三讲:循环输出表格1、for循环格式:for(循环变量的初始化; 循环条件; 循环变量的递增) {循环体}for循环适用于有规律的循环 此循环用的比较多2、while循环格式:while(条件){循环体}3、do while循环格式:do{循环体;}while(条件);while循环和do while循环的却别do while循环至少执行一次 而while循环可能一次都不会执行

2、for循环

<?php /**  循环输出一张表格  分别使用for循环while循环和do while循环  *///for循环//输出表格头echo "<table width='80%' align='center' border=1 cellpadding=5 cellspacing=0>";//外层循环控制行的输出for($i=1; $i<=5; $i++){//各行换色if($i%2 == 0){echo '<tr align="center" bgcolor="red">';}else{echo '<tr align="center">';}//内层循环控制列的输出for($j=1; $j<=8; $j++){echo '<td>'.$i*$j.'</td>';}echo '</tr>';}echo '</table>';?>

3、while循环

echo '<table width="80%" align="center" border=1 cellpadding=5 cellspacing=0>';$m = 1;while($m<=5){if($m%2==0){echo '<tr align="center" bgcolor="red">';}else{echo '<tr align="center">';}$n=1;while($n<=8){echo '<td>'.$m*$n.'</td>';$n++;}echo '</tr>';$m++;}echo '</table>';

4、do-while循环

echo '<table width="80%" align="center" border=1 cellpadding=5 cellspacing=0>';$k=1;do{if($k%2==0){echo '<tr align="center" bgcolor="red"> ';}else{echo '<tr align="center">';}$d=1;do{echo '<td>'.$k*$d.'</td>';$d++;}while($d<=8);$k++;}while($k<=5);//注意结尾的分号


0 0
原创粉丝点击