270 likes | 280 Views
PHP5&MySQL 程式設計. 第 3 章 控制結構. 3-1 認識控制結構. 判斷結構 (decision structures) if (if … 、 if … else … 、 if … elseif … ) switch 迴圈結構 (loop structures) for foreach while do … while. 3-2 if. 3-2-1 if :如果 … 就 … ( 單向選擇 ) if ( condition ) statement ; if ( condition ) { statement1 ;
E N D
PHP5&MySQL程式設計 第3章 控制結構
3-1 認識控制結構 • 判斷結構 (decision structures) • if (if…、if…else…、if…elseif…) • switch • 迴圈結構 (loop structures) • for • foreach • while • do…while
3-2if 3-2-1if:如果…就…(單向選擇) if (condition) statement; if (condition) { statement1; statement2; … statementN; }
\ch03\prac03-1.php <HTML> <HEAD> <TITLE> if... </TITLE> </HEAD> <BODY> <?php $a = 20; $b = 10; if ($a > $b) echo '$a比$b大'; ?> </BODY> </HTML>
3-2-2if…else…:如果…就…否則…(雙向選擇) if (condition) { statements1; } else { statements2; }
\ch03\prac03-2.php <HTML> <HEAD> <TITLE>if...else...</TITLE> </HEAD> <BODY> <?php $score = 59; if ($score > 60) echo '及格!'; else echo '不及格!'; ?> </BODY> </HTML>
3-2-3if…elseif…:「如果…就….否則如果…就…否則…」(多向選擇) if (condition1) { statements1 } elseif (condition2) { statements2 } elseif (condition3) { statements3 } … else { statementsN+1 }
\ch03\prac03-3.php <BODY> <?php $score = 85; if ($score >= 90) echo '優等!'; elseif ($score < 90 && $score >= 80) echo '甲等!'; elseif ($score < 80 && $score >= 70) echo '乙等!'; elseif ($score < 70 && $score >= 60) echo '丙等!'; else echo '不及格!'; ?> </BODY>
3-2-4if的替代語法 \ch03\prac03-4.php <?php $a = 20; $b = 20; if ($a > $b){ echo '$a比$b大'; echo '!'; }elseif ($a < $b){ echo '$a比$b小'; echo '!'; }else{ echo '$a等於$b'; echo '!'; } ?>
<?php $a = 20; $b = 20; if ($a > $b): echo '$a比$b大'; echo '!'; elseif ($a < $b): echo '$a比$b小'; echo '!'; else: echo '$a等於$b'; echo '!'; endif ?>
3-3switch switch(expression) { case value1: statements1; break; case value2: statements2; break; … default: statementsN+1 }
<?php $number = 3; switch($number) { case 1: echo 'ONE'; break; case 2: echo 'TWO'; break; case 3: echo 'THREE'; break; case 4: echo 'FOUR'; break; case 5: echo 'FIVE'; break; default: echo '數值超過範圍!'; } ?>
<?php $number = 3; if ($number == 1) echo 'ONE'; elseif ($number == 2) echo 'TWO'; elseif ($number == 3) echo 'THREE'; elseif ($number == 4) echo 'FOUR'; elseif ($number == 5) echo 'FIVE'; else echo '數值超過範圍!'; ?>
3-4for (計數迴圈) for (initializers; expression; iterators) { statements [break;] statements }
\ch03\prac03-8a.php <HTML> <HEAD> <TITLE>for</TITLE> </HEAD> <BODY> <?php for ($i = 1; $i <= 10; $i++) echo $i.'<BR>'; ?> </BODY> </HTML>
\ch03\prac03-8b.php <HTML> <HEAD> <TITLE>for</TITLE> </HEAD> <BODY> <?php for ($i = 1; ; $i++){ if ($i > 10) break; echo $i.'<BR>'; } ?> </BODY> </HTML>
\ch03\prac03-8c.php <HTML> <HEAD> <TITLE>for</TITLE> </HEAD> <BODY> <?php for (;;){ ++$i; if ($i > 10) break; echo $i.'<BR>'; } ?> </BODY> </HTML>
break敘述的妙用 \ch03\prac03-12.php 01:<HTML> 02: <HEAD> 03: <TITLE>for</TITLE> 04: </HEAD> 05: <BODY> 06: <?php 07: $result = 1; 08: for ($i = 1; $i <= 10; $i++){ 09: if ($i > 6) break; 10: $result = $result * $i; 11: } 12: echo $result; 13: ?> 14: </BODY> 15:</HTML>
3-5 條件式迴圈 3-5-1while while(condition) { statements [break;] statements }
\ch03\prac03-13.php <HTML> <HEAD> <TITLE>while</TITLE> </HEAD> <BODY> <?php while ($i < 10) echo ++$i.'<BR>'; ?> </BODY> </HTML>
3-5-2do…while do { statements [break;] statements }while(condition);
\ch03\prac03-14.php <HTML> <HEAD> <TITLE>do...while</TITLE> </HEAD> <BODY> <?php do echo ++$i.'<BR>'; while ($i < 10) ?> </BODY> </HTML>
3-5-3break與continue敘述 \ch03\prac03-15.php <HTML> <BODY> <?php for ($i = 1; $i <= 15; $i++) { if ($i <= 10) continue; echo $i.'<BR>'; } ?> </BODY> </HTML>
3-5-4exit()/die() 函式 • 例如: exit('檔案開啟失敗,程式終止執行!');
3-6foreach foreach (array_name as $value){ statements [break;] statements } foreach (array_name as $key => $value) { statements [break;] statements }
\ch03\prac03-16.php 01:<HTML> 02: <HEAD> 03: <TITLE>foreach</TITLE> 04: </HEAD> 05: <BODY> 06: <?php 07: $city = array('東京', '台北', '紐約'); 08: foreach ($city as $value) 09: echo $value.'<BR>'; 10: ?> 11: </BODY> 12:</HTML>
\ch03\prac03-17.php 01:<HTML> 02: <HEAD> 03: <TITLE>foreach</TITLE> 04: </HEAD> 05: <BODY> 06: <?php 07: $city = array('Japan' => '東京', 'Taiwan' => '台北', 'USA' => '紐約'); 08: foreach ($city as $key => $value) 09: echo '鍵:'.$key.';值:'.$value.'<BR>'; 10: ?> 11: </BODY> 12:</HTML>