PHP 入門指南 - 網站流程控制

switch 陳述 (statement) 可以用來判斷網頁傳回的 $_REQUEST 變數 (variable)




我們將 encryptor01.php 稍作簡單的修改,藉以說明整個概念
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html>
<head>
<title>Encode Software</title>
<link rel="stylesheet" href="encryptor.css">
</head>
<body>
<div class="menu">
<a href="<?PHP echo $_SERVER['PHP_SELF']; ?>">Home</a> -
<a href="<?PHP echo $_SERVER['PHP_SELF'].'?op=record'; ?>">Record</a> -
<a href="<?PHP echo $_SERVER['PHP_SELF'].'?op=about'; ?>">About</a>
</div>
<div class="main">
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="input" size="67"><br / >
<input type="submit" value="New" name="op">
<input type="submit" value="Load" name="op">
<input type="submit" value="Encode" name="op">
<input type="submit" value="Decode" name="op">
<input type="submit" value="Clean" name="op">
</form>
</div>
<div class="display">
<?php
switch ($_REQUEST["op"]) {
    case "New":
        echo "There is <b>New</b> button.";
        break;
     
    case "Load":
        echo "There is <b>Load</b> button.";
        break;
     
    case "Encode":
        echo "There is <b>Encode</b> button. Your input is '{$_POST['input']}'.";
        break;
     
    case "Decode":
        echo "There is <b>Decode</b> button. Your input is '{$_POST['input']}'.";
        break;
     
    case "Clean":
        echo "There is <b>Clean</b> button.";
        break;
     
    case "record":
        echo "There is <b><i>Record</i></b> page.";
        break;
     
 case "about":
        echo "There is <b><i>About</i></b> page.";
        break;
     
    default:
        echo "something happened";   
}
?>
</div>
</body>
</html>
 
<!-- 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:encryptor02.php
    功能:示範 PHP 程式
    作者:張凱慶
    時間:西元 2012 年 11 月 -->


這裡是將第三個區塊改成 PHP 程式,也就是加入 switch 陳述。由於選單中 Record 的 op 值為 "record" , About 為 "about" ,雖然傳回是 $_GET 變數,但都可以由 $_REQUEST 來取得。同樣的,表單中有五個 op ,其值分別為 "New" 、 "Load" 、 "Encode" 、 "Decode" 與 "Clean" ,由 $_POST 傳回也可由 $_REQUEST 取得
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
switch ($_REQUEST["op"]) {
    case "New":
        echo "There is <b>New</b> button.";
        break;
     
    case "Load":
        echo "There is <b>Load</b> button.";
        break;
     
    case "Encode":
        echo "There is <b>Encode</b> button. Your input is '{$_POST['input']}'.";
        break;
     
    case "Decode":
        echo "There is <b>Decode</b> button. Your input is '{$_POST['input']}'.";
        break;
     
    case "Clean":
        echo "There is <b>Clean</b> button.";
        break;
     
    case "record":
        echo "There is <b><i>Record</i></b> page.";
        break;
     
 case "about":
        echo "There is <b><i>About</i></b> page.";
        break;
     
    default:
        echo "something happened";   
}


程式很簡單,就在第三個區塊顯示傳回哪個 op 變數。 "Encode" 與 "Decode" 的地方要注意一下,這裡額外用 $_POST['input'] 取得文字輸入欄位的輸入值,並且將使用者輸入的文字印出來
33
34
35
36
37
38
39
case "Encode":
    echo "There is <b>Encode</b> button. Your input is '{$_POST['input']}'.";
    break;
     
case "Decode":
    echo "There is <b>Decode</b> button. Your input is '{$_POST['input']}'.";
    break;


PHP 的字串 (string) 裡也可用大括弧放變數,不然就要用字串連接運算子 . 。


至於選單 Home 或是第一次連結到 encryptor02.php 都會執行 default
53
54
default:
    echo "something happened";


來看看網頁結果吧!先看到 Record



New 按鈕



Encode 按鈕並輸入 "There is no spoon"



好了,網站流程控制的概念很簡單唄!接下來我們要將功能模組化,將 HTML 語法交給 PHP 函數 (function) ,來看看 make_page() 與 input_form() 吧!


中英文術語對照
陳述statement
變數variable
字串string
函數function


您可以繼續參考
網站篇


相關目錄
回 PHP 入門指南
回 PHP 教材
回首頁


參考資料
http://www.php.net/manual/en/tutorial.firstpage.php
http://www.php.net/manual/en/tutorial.useful.php
http://www.php.net/manual/en/tutorial.forms.php
http://php.net/manual/en/control-structures.switch.php

沒有留言: