整理 PHP 開發最高頻的核心函數分類速查表,涵蓋陣列、字串、日期時間、檔案系統四大領域,適合日常開發快速查閱。
| 函數 | 用途 | 範例/備註 |
|---|---|---|
| implode(glue, arr) | 陣列值連接為字串 | implode('-', [1,2,3]) → "1-2-3" |
| explode(sep, str) | 字串分割為陣列 | 別名 join |
| array_merge(arr1, arr2...) | 合併陣列 | 數值鍵重排,字串鍵後覆蓋前 |
| array_combine(keys, values) | 建立關聯陣列 | array_combine(['a','b'], [1,2]) → ['a'=>1,'b'=>2] |
| array_push(arr, val...) | 壓入尾部(堆疊) | 返回新長度 |
| array_unique(arr) | 移除重複值 | 保留首次出現,索引保留 |
| array_search(val, arr) | 搜尋值返回鍵 | 嚴格比較用 in_array |
| array_values(arr) | 取得所有值(重排索引) | |
| array_keys(arr) | 取得所有鍵 | |
| count(arr) / sizeof(arr) | 計算元素數 | |
| is_array(var) | 檢測是否陣列 | |
| sort(arr) | 排序(重排索引) | 引用修改,返回 bool |
| array_flip(arr) | 交換鍵值 | 值需為 int/string,可用於去重 |
| key(arr), current(arr), next(arr) | 內部指針操作 | 配合 while 遍歷 |
| reset(arr) | 指針歸零 | |
| array_chunk(arr, size, preserve_keys) | 分塊 | 返回多維陣列 |
| 函數 | 用途 |
|---|---|
| trim(str) | 移除首尾空白 |
| strlen(str) | 位元組長度 |
| substr(str, start, len) | 截取子字串 |
| strstr(haystack, needle) | 首次出現位置到結尾 |
| strpos(haystack, needle) | 首次出現位置索引 |
| str_replace(search, replace, subject) | 字串替換 |
| ucfirst(str) | 首字母大寫 |
| strtolower/strtoupper | 大小寫轉換 |
| ucwords(str) | 每個單詞首字母大寫 |
| strcmp(str1, str2) | 二進制安全比較 |
| substr_count(haystack, needle) | 子串出現次數 |
正則相關:
| 函數 | 用途 |
|---|---|
| preg_match(pattern, subject, &matches) | 單次匹配,$matches[0]完整,$matches[1]第一捕獲組 |
| preg_match_all(pattern, subject, &matches) | 全局匹配 |
| preg_replace(pattern, replacement, subject) | 搜尋替換 |
| preg_split(pattern, subject) | 正則分割 |
| preg_grep(pattern, array) | 陣列值匹配過濾 |
date(format, timestamp) 格式化字元速查:
| 字元 | 含義 | 範例 |
|---|---|---|
| Y | 4位年份 | 2024 |
| y | 2位年份 | 24 |
| m | 月份(帶前導0) | 01-12 |
| n | 月份(無前導0) | 1-12 |
| d | 日期(帶前導0) | 01-31 |
| j | 日期(無前導0) | 1-31 |
| F | 月份英文全名 | January |
| M | 月份縮寫 | Jan |
| H | 24小時(帶前導0) | 00-23 |
| G | 24小時(無前導0) | 0-23 |
| h | 12小時(帶前導0) | 01-12 |
| g | 12小時(無前導0) | 1-12 |
| i | 分鐘 | 00-59 |
| s | 秒 | 00-59 |
| a | am/pm 小寫 | am |
| A | AM/PM 大寫 | AM |
| L | 是否閏年 | 0/1 |
| l | 星期幾全名 | Monday |
| D | 星期縮寫 | Mon |
| w | 星期數字 | 0(日)-6(六) |
| W | 年間週數 | 01-53 |
| t | 當月天數 | 28-31 |
| z | 年間天數 | 0-365 |
| c | ISO8601 | 2024-01-15T10:30:00+08:00 |
| r | RFC2822 | Mon, 15 Jan 2024 10:30:00 +0800 |
| U | Unix 時間戳 | 1705285800 |
常用輔助函數:
checkdate(m, d, y):驗證日期合法性。getdate(ts):時間戳轉關聯陣列。mktime(h, i, s, m, d, y):構造時間戳。strtotime(str):英文文字時間轉時間戳(支援 "today", "+3 days" 等)。strftime(format, ts):本地化格式化(需 setlocale)。microtime():微秒級時間戳(效能測試)。gmdate/gmmktime:GMT 時間處理。date_default_timezone_set/get:設定/獲取預設時區(PHP 5.1+)。常用區間快捷計算:
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
$lastweek_start = date("Y-m-d", strtotime("monday last week"));
$lastweek_end = date("Y-m-d", strtotime("sunday last week"));
$thismonth_start = date("Y-m-01");
$thismonth_end = date("Y-m-t");
$thisquarter_start = date("Y-m-d", strtotime("first day of this quarter"));
$thisquarter_end = date("Y-m-d", strtotime("last day of this quarter"));
| 函數 | 用途 |
|---|---|
| file_exists(path) | 檔案/目錄是否存在 |
| filesize(path) | 檔案大小(位元組) |
| pathinfo(path) | 解析路徑返回 ['dirname','basename','extension','filename'] |
| mkdir(dir, mode, recursive) | 建立目錄 |
| rmdir(dir) | 刪除空目錄 |
| fopen(path, mode) | 開啟檔案指標 |
| fclose(fp) | 關閉檔案指標 |
| fwrite(fp, str, len) | 寫入檔案 |
| file_put_contents(path, data, flags) | 一次寫入檔案(支援 FILE_APPEND, LOCK_EX) |
| file_get_contents(path) | 一次讀取檔案內容 |
| fread(fp, len) | 讀取指定長度 |
| fgets(fp) | 讀取一行 |
| file(path) | 整個檔案讀入陣列(每行一元素) |
| readfile(path) | 讀取並直接輸出到輸出緩衝 |
| copy(src, dest) | 複製檔案 |
| unlink(path) | 刪除檔案 |
| rename(old, new) | 重命名/移動檔案或目錄 |
編碼轉換: iconv('GBK', 'UTF-8', $str) 或 mb_convert_encoding($str, 'UTF-8', 'GBK')。
將此頁面加入書籤,遇到「這個函數怎麼用?」時秒級定位,提升編碼效率。