PHP程式初探(2)_第1页
PHP程式初探(2)_第2页
PHP程式初探(2)_第3页
PHP程式初探(2)_第4页
PHP程式初探(2)_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、PHP程式初探(2) 時間:2009/06/03講師:劉道遠1程式流程控制結構 條件述句if 敘述elseelseifif (EXPRESSION) statement; if (EXPRESSION) statement; else statement; if (EXPRESSION) statement; elseif statement; else statement; 2程式流程控制結構 條件述句注意事項statement只有一行敘述的時候,可省略大括弧。以下的情形可改寫if ($a 1) $b = a;else $b = b;$b = ($a 1) ? a: b;3程式流程控制結構

2、switch 敘述基本語法switch (EXPRESSION) case expr_value: statement; break; default: statement; break;switch ($i) case apple: echo i is apple; break; case bar: echo i is bar; break; case cake: echo i is cake; break; default: echo Please make a new selection.; break;4程式流程控制結構 while 敘述基本語法注意事項:while 需要給予停止的條件

3、。while (EXPRESSION) statement; while (EXPRESSION) : statement; endwhile;$i = 1;while ($i = 10) echo $i+;5程式流程控制結構 do-while 敘述基本語法注意事項:do-while 至少執行一次。do statement; while (EXPRESSION);$a = false;while ($a = true) echo a is true; $a = false;$a = false;do() echo a is true; $a = false;while ($a = true);

4、6程式流程控制結構 for 敘述基本語法注意事項:EXPRESSION3是執行一次迴圈後執行。for (EXPRESSION1; EXPRESSION2; EXPRESSION3) statement; for ($i = 1; $i $value ) statement$a = array(1, 2, 3, 4);foreach ($a as $k = $v) echo $k: $v;8程式流程控制結構 break:指定跳出目前的迴圈continue:跳過某次的迴圈,執行下一次迴圈。$arr = array(one, two, three, four, stop, five);foreach

5、 ($arr as $k = $v) if ($v = stop) break; echo $v;foreach ($arr as $k = $v) if ($v = stop) continue; echo $v;9程式流程控制結構 include:將指定的檔案插到include的位置。require:和include一樣但是不可以用在判斷敘述或迴圈中。include_once:和include一樣,但指定引入的檔案只會被引入一次。require_once:和require一樣,但指定引入的檔案只會被引入一次。10陣列 陣列的初始化:array( key = value , . )$arr

6、= array(foo = bar, 12 = true);$arrkey = value;$arr = value;$arrfoo = bar;$arr12 = true;11陣列 陣列的操作:讀出陣列foreachforwhile$arr = array(one, two, three);foreach ($arr as $key = $value) echo Key: $key; Value: $value;for($key = 0; $key count($a); $key+) echo Key: $key; Value:$value;reset($arr); while (list(

7、$key, $value) = each($arr) echo Key: $key; Value:$value;12陣列 陣列的操作:幾個例子去除array中某個單元,並reindexunset、array_value$a = array(1 = one, 2 = two, 3 = three);unset($a2);/* will produce an array that would have been defined as $a = array(1 = one, 3 = three); and NOT $a = array(1 = one, 2 =three);*/$b = array_

8、values($a);/ Now $b is array(0 = one, 1 =three)13陣列 陣列的操作:幾個例子相同值的單元,只取出一個array_unique$input = array(a = green, red, b = green, blue, red);$result = array_unique($input);print_r($result);14陣列 陣列的操作:幾個例子push和poparray_push、array_pop$stack = array(orange, banana);array_push($stack, apple, raspberry);pr

9、int_r($stack);$stack = array(orange, banana, apple, raspberry);$fruit = array_pop($stack);print_r($stack);15陣列 陣列的操作:幾個例子將array的值送入某個函數array_mapfunction cube($n) return($n * $n * $n);$a = array(1, 2, 3, 4, 5);$b = array_map(cube, $a);print_r($b);16陣列 陣列的操作:幾個例子改變array值的方法/ PHP 5foreach ($colors as &

10、$color) $color = strtoupper($color);unset($color); /* ensure that following writes to$color will not modify the last array element */ Workaround for older versionsforeach ($colors as $key = $color) $colors$key = strtoupper($color);print_r($colors);17陣列 陣列的操作:幾個例子依據sub-array的index來排序請參照 online_progra

11、m/wfconfig.php 中 function sksortarray(peter = array(age = 21, gender = male), john = array(age = 19, gender = male),mary = array(age = 20, gender = female) );18字串處理及正規表達式 基本字串函數:字串中依據字元的操作$str = This is a test.;echo $first = $str0;/Techo $third = $str2;/i$str = This is still a test.;echo $last = $st

12、rstrlen($str)-1;/. $str = Look at the sea;$strstrlen($str)-1 = e;echo $str;/ Look at the see19字串處理及正規表達式 基本字串函數:substr 取得部份字串將字串 string 的第 start 字元起的字串取出 length 個字元;若 start 為負數,則從字串尾端算起。若可省略的參數 length 存在,但為負數,則表示取到倒數第 length 個字元。echo substr(abcdef, 1); / bcdefecho substr(abcdef, 1, 1); / becho subst

13、r(abcdef, 1, -1); / bcdeecho substr(abcdef, -1, 1); / fecho substr(abcdef, -1, -1); / star正不含start字元負含start字元length正往右取length負不含第 length個字元20字串處理及正規表達式 基本字串函數:去除字串的空白trim、ltrim、rtrim去除所有的空白$input = This is test string . ;$output = ;$input = trim($input);for($i=0;$istrlen($input);$i+) if(substr($inpu

14、t, $i, 1) != “”) $output .= trim(substr($input, $i, 1); else $output .= ;/這裡決定中間字元空格的大小 echo $output;21字串處理及正規表達式 基本字串函數:尋找某個字串是否含有特定字串,並回傳位置strpos$mystring = abc;$findme = a;$pos = strpos($mystring, $findme);if ($pos = false) echo The string $findme was not found in the string $mystring; else echo

15、The string $findme was found in the string $mystring; echo and exists at position $pos;22字串處理及正規表達式 正規表達式一套字串樣式比對的技述性語言檢查某字串是否完整對應到某樣式 (核對)在某字串中找尋對應樣式的子字串 (搜尋)由字串中取出對應到某樣式的字串 (取代)$string = This is test string .;$str = ereg_replace( +, , $string );echo $str;23字串處理及正規表達式 正規表達式行的開頭或結尾符號說明會比對匹配文章裡的每一行開頭

16、$ 會比對匹配文章裡的每一行結尾例子1:如果寫成 This ,則字串:This is a book符合這規則; Is this the book就不符合這規則。例子2:如果寫成 book$,則字串:This is a book、Is this the book都符合這規則。例子3:如果寫成 book$,則只有字串僅能有 book 這個單字,前後都不可以有其他文字出現。24字串處理及正規表達式 正規表達式字元集合符號說明集合比對字元集合(範圍)的一個字元。例子:如果寫成 H123456 或是 H1-6 ,則表示字串可以是 H1、H2、H3、H4、H5、H6 都符合這個規則。有關英文字母、數字及一

17、些標點符號可表示如下:a-z比對所有的小寫字母A-Z 比對所有的大寫字母a-zA-Z比對所有的字母0-9比對所有的數字0-9.- 比對所有的數字,句號和減號25字串處理及正規表達式 正規表達式非或排除字元集合這個部份前二項 與 規則的組合,但放的位置不同,其表達的意義也不同,所以特別說明如下:例子1:如果寫成 A-Z ,則表示字串開頭的字元是英文大寫字母成即符合規則,例如Z123456789。例子2:如果寫成 A-Z (注意 的位置),則表示字串開頭的字元不可以是英文大寫字母,例如Z123456789就不行,但是如果是 z123456789(小寫) 或 123456789Z(開頭是數字)都符合

18、這規則。26字串處理及正規表達式 正規表達式比對任意字元. 例子1:如果寫成 A.B ,則表示字串應是 A.B 才符合規則。(與前面介紹的規則相同)例子2:如果寫成 A.B ,則表示字串寫成 A1B 、 AzB 、 A+B 、. 都符合規則,也就是第 2 個字元是任意的。27字串處理及正規表達式 正規表達式特殊字元? 、範圍比對,利用 (大括號)n*n出現零次或多次n+ n出現一次以上n?n出現零次或一次n出現n次n,出現n次以上n,m出現n到m次28字串處理及正規表達式 正規表達式符號及 ( ) 括號|相當於OR的意義;例如 A(B|b)C 表示ABC或AbC都符合規則。() 指定字元序列,

19、簡單來說,就是一個字串,例如 (book|notebook)表示開頭文字字串只要是 book 或 notebook 都符合規則。29字串處理及正規表達式 正規表達式Special SequencesSpecial Sequencesw - Any “word” character (a-z 0-9 _) W - Any non “word” character s - Whitespace (space, tab CRLF) S - Any non whitepsace character d - Digits (0-9) D - Any non digit character . - (Pe

20、riod) Any character except newline 30字串處理及正規表達式 正規表達式Pattern ModifiersPattern Modifiersi - Case Insensitive m - Multiline mode - and $ match start and end of lines s - Dotall - . class includes newline x - Extended comments and whitespace e - preg_replace only enables evaluation of replacement as PH

21、P code S - Extra analysis of pattern U - Pattern is ungreedy u - Pattern is treated as UTF-831字串處理及正規表達式 正規表達式Point based assertionsPoint based assertionsb - Word boundary B - Not a word boundary A - Start of subject Z - End of subject or newline at end z - End of subject G - First matching position

22、 in subject32字串處理及正規表達式 正規表達式相關函數大致可分為三類:搜尋與比對 ereg()或preg_match() 搜尋並取代 ereg_replace()或preg_replace() 字串分割 split()或preg_split()preg_match一般來說效能比ereg好。Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be fast

23、er. 33字串處理及正規表達式 正規表達式例子:驗證email格式/S+wd.-2,.w2,6z/ialantw.結束不分大小寫34Session管理何謂session在使用者利用程式處理過程內,進行數個HTTP的要求與回應,這些要求與回應稱為session。保存session的方法利用cookie利用php的session功能35Session管理Cookie與session的運作原理Cookie的限制安全性的限制:使用這可以輕易看到或變更cookie的資料客戶端不支援(瀏覽器、安全性設定、proxy保護等),使cookie功能無法使用容量有限制36Session管理使用PHP的session功能透過session_start()函式初始化session。在$_SESSION裡登錄session變數。進行session變數處理。session的解除關閉瀏覽器並重新啟動session期限到在php.ini中設定session.cookie_lifetime解除session變數的登錄。用unset():解除某個變數session_destory()session_unset()$_SESSION = array()37Session管理例子設定$_SESSIONtestuser設定

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论