[PHP_Day 5]_基本語法part5_迴圈while_do..while
PHP迴圈
while
1 2 3 4 5 6 7 8 9 10 | <?php $count = 5; while($count !=0){ echo "<h1>My Test</h1>"; echo "Count: ".$count."<br/>"; $count--; } ?> |
Array遍歷(倒序)
1 2 3 4 5 6 7 8 9 10 | <?php $arr = ["blue","green","yello","white"]; $count = count($arr); echo $count."<br/>"; while($count--){ echo $arr[$count]."<br/>"; } ?> |
Array遍歷(正序)
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $arr = ["blue","green","yello","white"]; $count = count($arr); $idx = 0; echo $count."<br/>"; while($count--){ echo $arr[$idx]."<br/>"; $idx++; } ?> |
do ... while
do裡面會先做 while裡面要改成先減減不然會offset
1 2 3 4 5 6 7 8 9 10 11 | <?php $arr = ["blue","green","yello","white"]; $count = count($arr); $idx = 0; echo $count."<br/>"; do{ echo $arr[$idx]."<br/>"; $idx++; }while(--$count) ?> |
留言
張貼留言