[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)
?>











留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題