循環元分類2類
1 布爾型循環.
2 計數型循環php
布爾型
1.先判斷再循環
while(表達式){代碼塊}
2.先循環再判斷
do{代碼塊}while(表達式)html
循環 三要素:
1.循環的初始值.
2.循環的條件
3.循環的增量,能使條件知足的變量spa
$num = 1;
while($num < 10 ){
echo '我是第'.$num.'<br>';
$num++;
}
echo '<hr>';htm
//替代寫法
$num = 0;
while($num < 6):
echo $num;
$num++;
endwhile;
echo '<hr>';ci
//先循環在判斷
//先幹再說
$i = 9;
do{
echo $i;
$i--;
}while($i < 5 );it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- 1408 2016 -->
<select name="year" >
<option value="">請選擇....</option>io
<?php $year = 2016; ?>
<?php while($year >= 1408 ): ?>table
<option value=""><?php echo $year.'年'; ?></option>
<?php $year--; ?>變量
<?php endwhile; ?>select
</select>
</body>
</html>
echo '<table border="1" align="center" width="800" cellspacing="0">';
echo '<caption>10行10列 表格</caption>';
$i = 0;
while($i < 10){//負責輸出單元行
echo '<tr>';
$j = 0;
while($j < 10){//負責輸出單元格
echo '<td> </td>';
$j++;
}
echo '</tr>';
$i++;
}
echo '</table>';
echo '<table border="1" align="center" width="800" cellspacing="0">';
echo '<caption>for 10行10列 表格</caption>';
for($i=0;$i<10;$i++){
echo '<tr>';
for($j=0;$j<10;$j++){
echo '<td>'.($i *10 + $j).'</td>';
}
echo '</tr>'; } echo '</table>';