進階 WWW 程式設計 -- PHP 語言結構 靜宜大學資訊管理學系 蔡奇偉副教授 2003 2019/4/12 進階 WWW 程式設計 PHP 語言結構(二) 靜宜大學資訊管理學系 蔡奇偉副教授 2003 靜宜大學資管系 蔡奇偉編撰 版權所有 2003
敘述 條件敘述 迴圈敘述 return 敘述 include 敘述 require 敘述
條件敘述 if 敘述 if…else 敘述 elseif 敘述 switch 敘述
if 敘述 範例 if (condition) statement condition { statements } statements true false if (condition) statement { statements } 範例 if ($a > $b) { echo "a is bigger than b"; $b = $a; } if ($a > $b) echo "a is bigger than b";
if…else 敘述 範例 if (condition) { condition true_statements } else { false statements if (condition) { true_statements } else { false_statements 範例 if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; }
elseif 敘述 if (condition1) { true_statements_for_condition1 condition1 false S1 condition2 S2 conditionk Sk Sk+1 if (condition1) { true_statements_for_condition1 } elseif (condition2) { true_statements_for_condition2 … elseif (conditionk) { true_statements_for_conditionk else { all_false_statements
範例 if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; }
switch 敘述 switch (expression) { case label_1: label 必須是一個常式 statement(s); break; case label_2: … default : statement(s); } label 必須是一個常式
不使用 break 敘述時: switch ($i) { case 0: echo "i equals 0"; case 1: echo "i equals 1"; case 2: echo "i equals 2"; } if ($i == 0) { echo "i equals 0"; } if ($i == 1) { echo "i equals 1"; } if ($i == 2) { echo "i equals 2"; }
使用 break 敘述時: switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } if ($i == 0) { echo "i equals 0"; } esleif ($i == 1) { echo "i equals 1"; } elseif ($i == 2) { echo "i equals 2"; }
範例 switch ($i) { case 0: case 1: case 2: echo "i is less than 3 but not negative"; break; case 3: echo "i is 3"; }
範例 $string_match="second"; switch ($string_match) { case "first": case "second": case "third": echo "<H3>Something for all three</H3>"; switch ($string_match) { case "first": echo "something for first only"; break; case "second": case "third": echo "something for the other two"; break; } break; default: echo "<H3>no match</H3>"; }
迴圈敘述 while 敘述 do..while 敘述 for 敘述 foreach 敘述 break 敘述 continue 敘述
while 敘述 範例 while (condition) { condition statement(s) } Statement(s) false true while (condition) { statement(s) } 範例 $i = 1; while ($i < 0) { echo $i++; } $i = 1; while ($i <= 10) { echo $i++; } 輸出:無 輸出:1 2 … 10
do…while 敘述 範例 do { statement(s) statement(s) } while (condition) false true do { statement(s) } while (condition) 範例 $i = 0; do { echo $i; } while ($i > 0); $i = 0; do { echo $++i; } while ($i <= 10); 輸出:0 輸出:1 2 … 10 11
for 敘述 statement for ([initialExpr]; [condition]; [incrementExpr] ) { } initialExpr incrementExpr statement condition false true
範例 for ($i = 1; $i <= 10; $i++) { echo $i; } 1 $i = 1; for (;;) { if ($i > 10) { break; } echo $i; $i++; } for ($i = 1 ;; $i++) { if ($i > 10) { break; } echo $i; } 2 3 for ($i = 1; $i <= 10; echo $i++) ; 4 上面四個例子都輸出:1 2 … 10
foreach 敘述 foreach 敘述是專用來處理陣列的迴圈敘述,其格式如下: foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement 第一式用於索引陣列。每次進入迴圈時,取出陣列中的下一個元素存入變數 $value 中。 第二式用於對照陣列。每次進入迴圈時,取出陣列中的鍵值和對照值分別存入變數 $key 和 $value 中。
範例 /* foreach example : value only */ $a = array (1, 2, 3, 17); foreach ($a as $v) { echo "Current value of \$a: $v.\n"; } /* foreach example : key and value */ $a = array ( "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach($a as $k => $v) { echo "\$a[$k] => $v.\n"; } 範例
/. foreach example : dynamic arrays /* foreach example : dynamic arrays */ foreach(array(1, 2, 3, 4, 5) as $v) { echo "$v\n"; } /* foreach example : multi-dimensional arrays */ $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; foreach($a as $v1) { foreach ($v1 as $v2) { echo "$v2\n"; } } 範例 a = ((“a”, “b”), (“y”, “z”))
break 敘述 break; break n; 在迴圈中的 break 敘述是用來中斷迴圈的執行,然後跳出迴圈。在 switch 敘述中的 break 則是用來中斷 case 敘述的執行。 若未指定數字 n,則表示跳出目前的迴圈,否則,跳出 n 層的迴圈(或 switch)結構。
while (…) { … if (…) break; if (…) break 2; } // simple break goes here // break 2 goes here
範例 $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5<br>\n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting<br>\n"; break 2; /* Exit the switch and the while. */ default: break; } }
continue 敘述 continue; continue n; 若未指定數字 n,則表示跳出目前的迴圈,否則,跳出 n 層的迴圈結構。
while (…) { … if (…) continue; if (…) continue 2; }
範例 $i = 0; while ($i++ < 5) { echo "Outer<br>\n"; while (1) { echo " Middle<br>\n"; while (1) { echo " Inner<br>\n"; continue 3; } echo "This never gets output.<br>\n"; } echo "Neither does this.<br>\n"; }
return 敘述 範例 return; return expression; <?php function foo() { return "Cher"; } $name = foo(); echo $name; // ouput: Cher ?>
include 敘述 範例 include 敘述用來加入一個外部檔案。若加進來的檔案不存在的話,會產生警告的訊息。 vars.php test.php <?php $color = 'green'; $fruit = 'apple'; ?> <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?>
範例 include() through HTTP <?php /* This example assumes that www.example.com is configured to parse .php * * files and not .txt files. Also, 'Works' here means that the variables * * $foo and $bar are available within the included file. */ // Won't work; file.txt wasn't handled by www.example.com as PHP include 'http://www.example.com/file.txt?foo=1&bar=2'; // Won't work; looks for a file named 'file.php?foo=1&bar=2' on the // local filesystem. include 'file.php?foo=1&bar=2'; // Works. include 'http://www.example.com/file.php?foo=1&bar=2'; $foo = 1; $bar = 2; include 'file.txt'; // Works. include 'file.php'; // Works. ?>
範例 include() and conditional blocks <?php // This is WRONG and will not work as desired. if ($condition) include $file; else include $other; // This is CORRECT. if ($condition) { include $file; } else { include $other; } ?>
include_once 敘述 include_once 敘述基本上和前述的 include 敘述相同,惟一的的差別在於:若用 include_once 敘述,同一個檔案只會加進來一次。 範例 vars.php test.php <?php $color = 'green'; $fruit = 'apple'; ?> <?php echo "A $color $fruit"; // A include_once 'vars.php'; echo "A $color $fruit"; // A green apple $color = ‘red'; $fruit = ‘banana'; include_once 'vars.php'; echo "A $color $fruit"; // A red banana ?>
雖然在 include_once 敘述中,檔案名稱是大小寫有所區別的,但是有些作業系統(如 Windows)的檔案名稱卻沒有大小寫之分。這使得下面的例子: include_once("a.php"); include_once ("A.php"); 在 Windows 系統中會加進來檔案 a.php(或稱 A.php )兩次,這樣一來就和使用 include_once 的原意不合了。因此,敘述中的檔案名稱最好和外部的檔案名稱大小寫能夠一致,以避免上述的問題。
require 敘述和 require_once 敘述 requre 敘述和 include 敘述功能相同。它們惟一的不同處在於:欲加進來的檔案若不存在的話, include 敘述只會輸出警告的訊息,然後繼續執行程式,requre 敘述則視其為嚴重的錯誤而終止程式的執行。 requre_only 敘述和 include_only 敘述功能相同。它們惟一的不同處在於:欲加進來的檔案若不存在的話, include 敘述只會輸出警告的訊息,然後繼續執行程式,requre 敘述則視其為嚴重的錯誤而終止程式的執行。 因此,絕大多的時候,我們都使用 require 或 require_once。只用 include 或 include_once 來加入無關的緊要的檔案。