1 / 50

第一章 PHP 簡介

第一章 PHP 簡介. 鄧姚文 joseph.deng@gmail.com http://www.ywdeng.idv.tw. 使用 PHP 將 PHP 嵌入 HTML 加入動態內容 存取表單變數 識別字 使用者宣告變數 指派值給變數 PHP 的資料型別 變數的變數 變數的範圍. 運算子 使用運算子:產生表單加總 優先順序與結合性:執行運算式 變數函式 控制架構 以 Conditionals 決策 迴圈:重複的動作 跳出控制結構或命令. 大綱. 使用 PHP. 應用範例:訂購單

Download Presentation

第一章 PHP 簡介

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第一章 PHP 簡介 鄧姚文 joseph.deng@gmail.com http://www.ywdeng.idv.tw

  2. 使用 PHP 將 PHP 嵌入 HTML 加入動態內容 存取表單變數 識別字 使用者宣告變數 指派值給變數 PHP 的資料型別 變數的變數 變數的範圍 運算子 使用運算子:產生表單加總 優先順序與結合性:執行運算式 變數函式 控制架構 以 Conditionals 決策 迴圈:重複的動作 跳出控制結構或命令 大綱

  3. 使用 PHP • 應用範例:訂購單 • 以 HTML <form action="…"> 將輸入傳遞給 PHP 網頁處理 • 在 HTML 之中 • <input name='name'> • 在 PHP 網頁中 • $name 即傳遞過來的輸入值

  4. 將 PHP 嵌入 HTML • Short style • <?echo "<P>Order processing …";?> • XML style • <?phpecho "<P>Order processing …";?>

  5. 將 PHP 嵌入 HTML • SCRIPT style • <SCRIPT Language="php">echo "<P>Order processing …";</SCRIPT> • ASP style • <%echo "<P>Order processing …";%> • 使用那一種 Style 與 Server 設定有關,也與個人習慣有關

  6. 空白 • 一個空格、一百個空格、一個空行、一百個空行都是空白 • 對瀏覽器而言,都只是一個空白 • 對 PHP 引擎而言也是如此

  7. 註解 • /* … 多行註解 */ • // 單行註解 • # 單行註解

  8. 網頁以 PHP 加入動態內容 • echo • 用來輸出字串 • date() • 取得日期時間 • 參考文件 • http://www.php.net • 練習: • 輸出一個網頁,顯示現在日期與時間,格式用 Y-m-d H:i:s

  9. 存取表單變數 • 以 $name = $_POST['name'] 取得 action=post 傳遞的變數 • 以 $name = $_GET["name"] 取得 action=get 傳遞的變數 • 兩頁式 • 一個網頁是 HTML(<form>) • 一個網頁是 PHP(action 的目的地) • 單頁式 • 用 empty($name) 判斷目前是什麼狀態

  10. 練習:Say Hello • 以HTML表單請使用者輸入姓名 • 顯示 Say Hello 的畫面向使用者打招呼 • 能否用 JavaScript 驗證使用者是否確實輸入姓名? • 以兩頁式實現 • 以單頁式實現

  11. 字串連結 • 以 . 連結字串 • <?php echo $tiresqty . " tires"; ?> • 另一種用法 • <?php echo "$tiresqty tires"; ?> • 雙引號括起來的字串,裡面的變數會被取代 • 單引號括起來的字串,不做變數取代

  12. 使用者宣告變數 • 變數一定以 $ 開頭 • 大小寫視為不同 • 變數的資料型別視初始值而定 • $totalqty = 100; // 整數 • $total = 2340.0; // 浮點數

  13. 變數的型別 • 變數型別 • 整數(integer) • 浮點數(double) • 實數 • 字串(string) • 陣列(array) • 物件(object) • 弱型別:變數的型別隨時依指定的值而變 • 型別轉換 • $total = (double)$totalqty;

  14. 變數的變數 • $varname = "tireqty"; • $$varname 即 $tireqty

  15. 常數 • define("TIREPRICE", 100); • echo TIREPRICE; • 常數不需要加 $ • 用 phpinfo() 顯示內建常數

  16. 變數範圍 • 在函式內的都是區域變數 • 指名全域變數用 global • 系統全域變數

  17. 運算子 • 數學運算 • 加 + • 減 - • 乘 * • 除 / • 整數除法取餘數 % • 字串運算 • 字串連結 .

  18. 運算子 • 指派 = • $b = 6 + ($a = 5); • $a 為 5 • $b 為 11 • 複合指派 • $b += $a • 相當於 $b = $b + $a

  19. 運算子 • 遞增 ++ • $a = 100; • $b = $a++; // $b 為 100, $a 為 101 • $c = ++$a; // $c 為 102, $a 為 102 • 遞減 --

  20. 運算子 • 參考 • $a = 5; • $b = $a; • $c = &$a; • $a++; • $a 變成 6 • $b 不變為 5 • $c 也是 6

  21. 運算子 • 關係運算子 • 相等 == • 全等 === :運算元相等且型別相同 • 不相等 != 或 <> • 小於 < • 大於 > • 小於等於 <= • 大於等於 >=

  22. 運算子 • 邏輯運算子 • NOT ! • AND && 或 and • OR || 或 or

  23. 運算子 • 位元運算子 • AND & • OR | • NOT ~ • XOR ^ • 左移 << • 右移 >>

  24. 運算子 • 三元運算子 • echo ($sex=="男") ? "先生" : "小姐"; • 錯誤抑制 • $a = @(100/0); • 不顯示錯誤訊息 • 執行運算子 • $out = `ls –al`; • echo "<pre>$out</pre>";

  25. 練習:Say Hello 續集 • Say Hello 加入性別判斷 • 向男性打招呼時加入『先生』,向女性打招呼時加入『小姐』 • 以 Radio Button 取得性別輸入

  26. 優先順序與結合性 • See PHP Operator Precedence • 別死記,善用()

  27. 變數函式 • gettype() 取得變數型別 • integer • double • string • array • object • settype() 設定變數型別 • settype($a, 'double');

  28. 變數函式 • 測試變數狀態 • isset() 變數是否存在? • unset() 取消變數 • empty() 是否為空值? • 重新解釋變數 • int intval() • double doubleval() • string strval()

  29. 控制架構 • Conditionals • if • 程式區塊 {} • else • elseif • 同 else if

  30. <tr valign="top"> <td><font color="red">*</font></td> <td>性  別:</td> <?php if (!empty($sex) && ($sex == "女")) { ?> <td><input type="radio" name="sex" value="男" tabindex="8">男&nbsp; <input type="radio" name="sex" value="女" checked tabindex="9">女</td> <?php } else { ?> <td><input type="radio" name="sex" value="男" checked tabindex="8">男&nbsp; <input type="radio" name="sex" value="女" tabindex="9">女</td> <?php } ?> </tr>

  31. switch 敘述 • switch (簡單型別變數) {case …} • break • 跳出 switch 敘述 • default: • 所有的 case 都不合時使用

  32. while 迴圈 • while (condition) expression;

  33. 以 while 迴圈產生運費表格

  34. for 迴圈 • for (expression1; condition; expression2)expression3;

  35. do … while 迴圈 • doexpression;while (condition);

  36. 跳出控制結構或命令 • break; • 跳出 switch … case 或 迴圈 • continue; • 跳過迴圈這一回 • exit; • 程式結束

  37. HTML Form 複習 • method • get: 參數列出於 URL 上 • post: 隱密性較高,參數長度不受限 • name: • Form 的名字,任何網頁元件若需要以 JavaScript 控制,都需要一個名字 • action • 一個網頁,處理 Form 傳送出去後的資料 <form method="post" name="frmApply" action="member_apply_verify.php">

  38. <input> • <input type="submit" value=…> • 送出表單 • Value= • 按鈕上出現的文字 • <input type="reset" value=…> • 將所有元件的值回復成預設值(清除或歸零) • Value= • 按鈕上出現的文字

  39. <input type="button"> • <input type="button" value="送出" onClick="verifyInput()"> • 一個普通的按鈕 • 按鈕按下之後採取的動作由 OnClick= 設定 • OnClick= 之後的動作由 JavaScript 撰寫

  40. <input type="text"> • <input type="text" name="soc_id" title="請輸入考生身份證字號" size="10" maxlength="10" tabindex="1" value="<?php echo value_or_empty($soc_id); ?>"> • 文字輸入方塊 • title 為提示文字,鼠標置於其上時顯示 • size 文字方塊大小 • maxlength 最大長度,單位為"字",但是不同的 browse 有不同的解釋

  41. <input type="password"> • <input type="password" name="password" title="請設定登入密碼" tabindex="3" value="<?php echo value_or_empty($password); ?>"> • 輸入的文字以 * 顯示

  42. <input type="hidden"> • <input type="hidden" name="soc_id" value="<?php echo strtoupper($soc_id); ?>"> • 網頁上不顯示,不輸入,但是回隨著 Form 傳送出去 • Authors generally use this control type to store information between client/server exchanges that would otherwise be lost due to the stateless nature of HTTP

  43. <input type="radio"> • <input type="radio" name="sex" value="男" checked tabindex="8">男<input type="radio" name="sex" value="女" tabindex="9">女 • name 相同的形成一組 • 單一選擇 • 表單傳送後以 $sex 取得選取選項的值(value)

  44. <td>性  別:</td> <?php if (!empty($sex) && ($sex == "女")) { ?> <td><input type="radio" name="sex" value="男" tabindex="8">男&nbsp; <input type="radio" name="sex" value="女" checked tabindex="9">女</td> <?php } else { ?> <td><input type="radio" name="sex" value="男" checked tabindex="8">男&nbsp; <input type="radio" name="sex" value="女" tabindex="9">女</td> <?php } ?>

  45. <select> • 下拉式選單 • 選項為 <option>

  46. <select name="prio1" size="1" tabindex="60"> <option selected value='00'> </option> <option value='01'>企業管理學系</option> <option value='02'>國際企業學系</option> <option value='03'>會計學系</option> <option value='04'>航運與物流管理學系</option> <option value='05'>財務金融學系</option> <option value='06'>觀光與餐飲旅館學系</option> <option value='07'>空運管理學系</option> <option value='08'>資訊管理學系</option> <option value='09'>資訊及電子商務學系</option> <option value='11'>應用外國語文學系英語組</option> </select>

  47. 練習:留言版 • 實作一個留言板,練習各種 HTML FORM 輸入

More Related