PHP Build-in Functions
PHP內建函式
說明:
- 函式內參數如果是用中括號「[」、「]」包圍住,代表選用,可傳也可沒有傳入。
- 參數內mixed型別並不是真正型別,而是代表其該參數支援多種型別。
快速檢索
一般函式:
var_dump()
、
header()
、
exit()
、
die()
、
sleep()
、
isset()
、
unset()
、
function_exists()
、
mt_rand()
、
mt_srand()
、
陣列函式:
字串輸出與調整:
-
- void echo(string $arg1 [, string $... ]);
- 輸出字串,可以輸出多個字串,以逗號「,」隔開,小括號可以省略。
-
- int print(string $arg);
- 輸出字串,只能輸出一個字串,小括號可以省略。
-
- string nl2br(string $string [, bool $is_xhtml = true ]);
- 以HTML的<br/>來取代換行字元「\n」。
-
- string strip_tags(string $str [, string $allowable_tags ]);
- 移除字串中的HTML和PHP標籤。
-
- string addslashes(string $str);
- 將字串中的「'」、「"」、「\」和「NULL(空字元)」4個字元前面加上跳脫字元「\」。
-
- string trim(string $str [, string $character_mask = " \t\n\r\0\x0B" ]);
- 將字串開頭和結尾的空白移除。
-
- string ltrim( string $str [, string $character_mask ]);
- 將字串開頭的空白字元移除。
-
- string rtrim(string $str [, string $character_mask ]);
- 將字串結尾的空白字元移除。
字串分割與合併:
-
- string strtok(string $str, string $token);
string strtok(string $token); - 指定分隔字元對字串做分割,只有第一次呼叫需要設定第一個字串參數,之後都不用,設了第一個字串參數表示要重來。
- string strtok(string $str, string $token);
-
- array explode(string $delimiter, string $string[, int $limit]);
- 指定分格字元並將字串分割成陣列,如果有指定第三個分割的數量參數,超過的字串部分會全部被放在陣列最後一個元素。
-
- array str_split(string $string[, int $split_length = 1]);
- 指定字元數,分隔成字串陣列,如果沒有指定,則預設是一個字元。
-
- string implode(string $glue, array $pieces);
string implode(array $pieces); - 將陣列的元素連結起來成為字串。
- string implode(string $glue, array $pieces);
-
- string join(string $glue, array $pieces);
string implode(array $pieces); - 將陣列的元素連結起來成為字串(與implode相同)。
- string join(string $glue, array $pieces);
字串查詢:
-
- int strlen(string $string);
- 計算字串中有幾個字元(不支援中文字之類的雙字元組)。
-
- int mb_strlen(string $string);
- 計算字串中有幾個字元(支援中文字之類的雙字元組)。
-
- int substr_count( string $haystack , string $needle [, int $offset = 0 [, int $length ]] );
- 查詢設定的字串在在整個字串中出現的次數。
-
- mixed str_word_count( string $string [, int $format = 0 [, string $charlist]]);
- 查詢字串中每個單字出現的次數。(第二個參數沒設定會回傳總共幾個單字,設1=傳回陣列,每個元素為一個單字出現次數,2=回傳每個單字出現位置為索引,單字為值的陣列。)
-
- mixed strpos(string $haystack , mixed $needle [, int $offset = 0 ]);
- 查詢字元在字串中第一次出現的位置,由0開始。
-
- int strrpos( string $haystack , string $needle [, int $offset = 0 ]);
- 查詢字元在字串中最後一次出現的位置,由0開始。
字串取代與截取:
-
- string strstr(string $haystack, mixed $needle [, bool $before_needle = false ]);
- 截取$needle字元在$haystack字串中第一次出現到最後的字串。
-
- string stristr(string $haystack, mixed $needle [, bool $before_needle = false ]);
- 截取$needle字元在$haystack字串中第一次出現到最後的字串,不分大小寫。
-
- string strrchr(string $haystack , mixed $needle);
- 截取$needle字元在$haystack字串中第最後一次出現到最後的字串
-
- string substr(string $string, int $start [, int $length ]);
- 截取字串中指定位置和數量的子字串,如果沒有指定數量(第三參數),則截取到最後。
-
- mixed str_replace(mixed $search , mixed $replace , mixed $subject [, int &$count ]);
- 將$subject字串中的某個子字$search串替代為指定的字串$replace。
-
- mixed str_ireplace(mixed $search , mixed $replace , mixed $subject [, int &$count ]);
- 將$subject字串中的某個子字$search串替代為指定的字串$replace,不分大小寫。
-
- mixed substr_replace(mixed $string , mixed $replacement , mixed $start [, mixed $length ]);
- 指定字串$string位置$start,以設定的字串$replace,取代指定數量$length,若無指定數量,會取代到字串最後。
-
- string str_pad(string $input, int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT]]);
- 將字串用指定的字元填滿到指定的長度(STR_PAD_RIGHT=向右填滿(預設),STR_PAD_LEFT=向左填滿,STR_PAD_BOTH=兩邊平均填滿)。
-
- string str_repeat(string $input , int $multiplier);
- 指定次$multiplier數來重複字串$input。
-
- string strrev(string $string);
- 顛倒字串內的字元(不支援中文之類的雙字元組)。
字串格式化:
字串分析:
字串轉換:
-
- string strtolower(string $string);
- 將字串內英文全部轉成小寫。
-
- string strtoupper(string $string);
- 將字串內英文全部轉成大寫。
-
- string ucfirst(string $str);
- 將字串內第一個英文單字的第一個字母轉成大寫。
-
- string ucwords(string $str);
- 將字串內每一個英文單字的第一個字母轉成大寫。
-
- string htmlspecialchars(string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]]);
- 將特殊字元轉換為HTML的實體參照(樣式:ENT_COMPAT=只轉換「"」,ENT_QUOTES=只轉換「'」,ENT_NOQUOTES=「"」「'」都轉換。。
-
- string htmlentities(string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]]);
- 轉換所有可能有HTML實體參照的字元成為HTML實體參照。
-
- string htmlspecialchars_decode(string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ]);
- 將HTML實體參照轉回特殊字元。
字串比對:
-
- int strcmp(string $str1, string $str2);
- 字串比較大小。
-
- int strcasecmp(string $str1, string $str2);
- 字串比較大小,不分大小寫。
-
- int strnatcmp(string $str1, string $str2);
- 使用自然順序演算法比較字串大小。
-
- int strcasecmp(string $str1, string $str2);
- 使用自然順序演算法比較字串大小,不分大小寫。
字串比對結果值:
條件 回傳值 字串1< 字串2 -1 字串1> 字串2 1 字串1== 字串2 0
何謂自然順序:
一般在比較字串時,都是由左到右一個字元一個字元來比,所以當遇到img2.jpg和img10.jpg,會出現1的結果(也就是img2.jpg大於img10.jpg),但實際上應該是img10.jpg大於img2.jpg;而自然順序演算法會將數字整個一起做比較,因此用自然順序比較的情況下,會得到—1的結果(也就是img2.jpg小於img10.jpg)。
字串加密:
- string crypt(string $str [, string $salt]);
- 使用DES加密方式將字串編碼後返回。
- string md5(string $str [, bool $raw_output = false ]);
- 使用MD5演算法計算雜湊值後回傳。(無法轉回原字串)
- string sha1(string $str [, bool $raw_output = false ]);
- 使用SHA1演算法計算雜湊值後回傳。(無法轉回原字串)
header()
在HTTP通訊協定當中,header的部分定義網頁的相關資訊(如:文件類型、語言等等),header()函式可以用來自行定義這些資訊。
header(要定義的屬性字串 [, 是否取代原本設定 [, HTTP回應碼]]);
備註:header()必須放在網頁輸出任何內容之前(也就是在所有的echo之前,甚至<html>標籤之前),否則會導致錯誤,你可以將它放在程式碼的第一行。
範例:
// 頁面重新導向(Location):讓瀏覽器前往指定的網址 header("Location: http://www.aaronlife.com"); // 重新整理網頁(Refresh),透過程式來自動重新整理網頁,也可以設定要重新整理的時間間隔 header("Refresh: 5"); // 每5秒就重新整理一次網頁 header("Refresh: 3; URL=http://www.aaronlife.com"); // 3秒後重新更新網頁,並前往網址:http://www.aaronlife.com // 定義網頁編碼(Content-type),用Content-type定義網頁編碼 header("Content-type: text/html; charset=utf-8"); // UTF-8編碼 header("Content-type: text/html; charset=big5"); // BIG5編碼 // 用Content-type定義輸出的檔案格式(MIME Type) header("Content-type: application/pdf;"); // PDF檔 header("Content-type: application/zip;"); // ZIP壓縮檔 header("Content-type: video/mov;"); // MOV影片檔 header("Content-type: video/avi;"); // AVI影片檔 header("Content-type: audio/mpeg3;"); // MP3聲音檔 header("Content-type: audio/x-wav;"); // WAV聲音檔 header("Content-type: image/jpg;"); // JPG圖片 header("Content-type: image/png;"); // PNG圖片 header("Content-type: image/gif;"); // GIF圖片
關於網際網路的檔案格式(MIME Type)定義,可以參考維基百科:MIME(多用途網際網路郵件擴展)
exit()、die()
在某些情況下,如果自行停止程式的執行,可以呼叫die()或exit()函式並輸出錯誤訊息,這兩個函式功能是一樣的。
void die([string $status]); void die(int $status); void exit([string $status]); void exit(int $status);
範例:
die('無法連接資料庫!'); exit('檔案開啟失敗!');
sleep()
延遲一段時間後再繼續執行程式。
int sleep(int $seconds);
範例:
<?php // 顯示目前系統時間 echo date('h:i:s').'<br/>'; // 延遲5秒 sleep(5); // 再次顯示目前系統時間 echo date('h:i:s').'<br/>'; ?>
isset()
判斷變數是否有定義並且其值不是空值(NULL)。
bool isset (mixed $var [, mixed $... ]);
範例:
echo isset($a); echo isset($a, $b);
unset()
描述:刪除已經定義的變數,可以ㄧ次刪除多個。
unset(變數 [, 變數 ...]);
範例:
unset($a); unset($b, $c);
count()
取得陣列內元素的數量。
int count(mixed $array_or_countable [, int $mode = COUNT_NORMAL]);
sizeof()
取得陣列內元素的數量。
int sizeof(mixed $array_or_countable [, int $mode = COUNT_NORMAL]);
function_exists()
檢查函式是否存在。
bool function_exists(string $function_name);
mt_rand()
產生更好的亂數。
int mt_rand(void); int mt_rand(int $min, int $max);
mt_srand()
設定亂數種子,用來取得更隨機的亂數。
void mt_srand([int $seed]);
範例:
mt_srand((double)microtime() * 1000000); // 設定亂數種子,用來取得更隨機的亂數 $val = mt_rand(1, 5); // 產生1~5之間亂數
var_dump()
將變數的資訊詳細顯示出來。
void var_dump(mixed $expression [, mixed $... ]);
通常用在開發中將變數或陣列內的資訊詳細的顯示出來,方便程式除錯。