共計整理150個函數
一、數學函數
11個
函數 | 說明 | 舉例 | 結果 |
abs | 絕對值 | abs(45), abs(-45) | 45, 45 |
ceil | 向上取整 | ceil(5.5) | 6 |
floor | 向下取整 | floor(5.5) | 5 |
fmod | 浮點數取餘 | fmod(5, 3) | 2 |
pow | n次方運算 | pow(2, 3) | 8 |
round | 四舍五入 | round(4.55555, 2) | 4.56 |
sqrt | 求平方根 | sqrt(9) | 3 |
max | 最大值 | max(1, 2, 3), max(array(1, 2, 3)) | 3, 3 |
min | 最小值 | min(1, 2, 3), min(array(2, 3, 4)) | 1, 1 |
rand | 随機數 | rand(1, 4), mt_rand(1, 4) | 4, 1 |
pi | 圓周率值 | pi() | 3.1415926535898 |
二、字元串函數
57個
1、字元串處理
13個
trim | 修剪兩邊 | trim("\thello\t") | hello |
ltrim | 修剪左邊 | ltrim("\thello\t") | hello\t |
rtrim | 修剪右邊 | rtrim("\thello\t") | \thello |
str_pad | 字元串填充 | str_pad(“hello”, 10, “*”) | hello***** |
str_repeat | 重複字元串 | str_repeat(“hello”, 2) | hellohello |
strrev | 反轉字元串 | strrev(“hello”) | olleh |
str_shuffle | 打亂字元串 | str_shuffle(“hello”) | lheol |
number_format | 格式化數字 | number_format(100000.897, 2, “.”, “-”) | 100-000.90 |
strtolower | 字元串轉為小寫 | strtolower(“Hello”) | |
strtoupper | 字元串轉為大寫 | strtoupper(“hello”) | HELLO |
ucfirst | 字元串首字母大寫 | ucfirst(“hello world”) | Hello world |
ucwords | 字元串所有首字元大寫 | ucwords(“hello world”) | Hello World |
wordwrap | 字元串折行 | wordwrap(“hello world”, 5) | hello\nworld |
3、字元串比較
8個
chr | ASCII 值傳回字元 | chr(65) | A |
ord | 字元串第一個字元的ASCII值 | ord(“hello”) | 104 |
strcasecmp | 不區分大小寫比較兩字元串 | strcasecmp(“Hello”, “HELLO”) | |
strcmp | 區分大小寫比較兩字元串 | strcmp(“Hello”, “HELLO”) | 32 |
strncmp | 比較字元串前n個字元,區分大小寫 | strncmp(“Hello”, “HELLO”, 3) | |
strncasecmp | 比較字元串前n個字元,不區分大小寫 | strncasecmp(“Hello”, “HELLO”, 3) | |
strnatcmp | 自然順序法比較字元串長度,區分大小寫 | (“Hello”, “HELLO”) | 1 |
strnatcasecmp | 自然順序法比較字元串長度, 不區分大小寫 | strnatcasecmp(“Hello”, “HELLO”) |
4、字元串切割與拼接
6個
chunk_split | 将字元串分成小塊 | chunk_split(“hello world”, 3) | hel lo wor ld |
strtok | 切開字元串 | strtok(“hello world”, " ") | |
explode | 字元串分割 | explode(" ", “hello world”) | Array([0] => hello [1] => world) |
str_split | str_split(“hello”) | Array([0] => h [1] => e…) | |
implode | 将數組值連接配接成字元串 | implode("+", array(“hello”, “world”)) | hello+world |
substr | 截取字元串 | substr(“hello”, 1, 3) | ell |
5、字元串查找替換
15個
str_replace | 字元串替換,區分大小寫 | str_replace(“h”, “xx”, “hello”) | xxello |
str_ireplace | 字元串替換,不區分大小寫 | str_ireplace(“h”, “xx”, “hello”) | |
substr_replace | 字元串替換 | substr_replace(“hello, hello”, “x”, 1, 6) | hxhello |
similar_text | 兩字元串相同字元的數量 | similar_text(“hello”, “Hello”) | |
strstr | 字元串開始位置到結束的字元串 | strstr(“HELLO hello”, “l”) | llo |
strchr | strstr()的别名 | strchr(“HELLO hello”, “l”) | |
stristr | 字元串開始位置到結束的字元串,不區分大小寫 | stristr(“HELLO, hello”, “l”) | LLO, hello |
strrchr | 字元串最後一次出現位置開始到末尾的字元串 | strrchr(“hello”, “l”) | lo |
strtr | 轉換字元串中的某些字元 | strtr(“hello”, “ll”, “xx”) | hexxo |
strpos | 字元最先出現的位置 | strpos(“HELLO, hello”, “l”) | 9 |
stripos | 字元最先出現的位置,不區分大小寫 | stripos(“HELLO, hello”, “l”) | |
strrpos | 字元最後出現的位置 | strrpos(“hello,HELLO”, “l”) | |
strripos | 字元最後出現的位置,不區分大小寫 | strripos(“hello, HELLO”, “l”) | 10 |
strspn | 字元串中首次符合mask的長度 | strspn(“www.baidu.com”, “ad”) | |
strcspn | 字元串中不符合mask的長度 | strcspn(“www.baidu.com”, “ad”) |
6、字元串統計
4個
substr_count | 統計字元串出現次數 | substr_count(“hello”, “l”) | |
str_word_count | 統計含有的單詞數 | str_word_count(“hello world”) | |
strlen | 統計字元串長度 | strlen(“hello world”) | 11 |
count_chars | 統計字母次數 | count_chars(“hello”)) | Array([0] => 0 [1] => 0… [255] => 0) |
7、字元串其他函數
1個
md5 | 字元串md5編碼 | md5(“hello”) | 5d41402abc4b2a76b9719d911017c592 |
三、數組函數
33個
1、數組建立
5個
array | 數組建立 | array(“Dog”, “Cat”) | Array([0]=>Dog [1]=>Cat) |
array_combine | 兩個數組zip生成一個數組 | array_combine(array(“a”), array(“Cat”)) | Array([a]=>Cat) |
range | 定範圍的數組 | range(0, 4, 2) | Array([0]=>0 [1]=>2 [2]=>4) |
compact | 建立數組,變量名為鍵,變量值為值 | $name= “Tom”; $age = “38”;compact(“name”, “age”) | Array([name]=>Tom [age]=>38) |
array_fill | 填充值生成數組 | array_fill(2, 1, “Dog”) | Array([1]=>Dog) |
2、數組合并和拆分
array_chunk | 重組數組塊 | $a=array("a"=>"Cat", "b"=>"Dog"); array_chunk($a, 1) | Array([0] => Array([0] => Cat) [1]=>Array([0]=>Dog)) |
array_merge | 合并 | array_merge(array("a"=>"Dog"), array("b"=>"Cat")) | Array([a]=>Dog [b]=>Cat) |
array_slice | 切片 | array_slice(array(0=>"Dog",1=>"Cat"), 1, 1) | Array([0] => Dog) |
array_diff | 差集 | array_diff(array(0=>"Cat",1=>"Dog"), array(0=>"Dog")) | Array([0]=>Cat) |
array_intersect | 交集 | array_intersect(array(0=>"Cat"), array(1=>"Dog")) | Array([1]=>Cat [2]=>Dog) |
3、數組查找替換
array_search | 查找值,傳回鍵,沒有傳回假 | array_search(“Dog”, array(“a”=>“Dog”)) | a |
array_splice | 數組替代 | array(0=>“Dog”, 1=>“Cat”), 0, 1, array(0=>“Tiger”) | array(0=>“Tiger”) |
array_sum | 求和 | array_sum(array(0=>5, 1=>10)) | 15 |
in_array | 查找值 ,區分大小寫 | in_array(“Tom”, array(“Tom”, “Jack”)) | |
array_key_exists | 查找鍵 | array_key_exists(“b”, array(“b”=>“Cat”)) |
4、數組元素操作
10個
key | 指針目前指向的鍵名 | key(array(“a”=>“Dog”,“b”=>“Cat”)) | |
current | 目前元素 | current(array(“a”=>“Dog”,“b”=>“Cat”)) | Dog |
next | 下一進制素 | next(array(“a”=>“Dog”,“b”=>“Cat”)) | Cat |
prev | 上一進制素 | prev(array(“a”=>“Dog”,“b”=>“Cat”)) Dog | |
end | 指向最後一個元素 | end(array(“a”=>“Dog”,“b”=>“Cat”)) | |
reset | 指向第一個元素 | reset(array(“a”=>“Dog”,“b”=>“Cat”)) | |
list | 序列解包 | list(a , a,a,b)=array(“Dog”,“Cat”) | $a=“Dog”; $b=“Cat” |
array_shift | 傳回删除的第一個元素 | array_shift(array(“Dog”,“Cat”)) | |
array_unshift | 開頭插入元素 | array_unshift(&array(),“Dog”) | Array([0]=>“Dog”) |
array_push | 最後壓入元素 | array_push(&array(), “Dog”) | |
array_pop | 彈出最後一個元素 | array_pop(&array(), “Dog”) | |
shuffle | 數組打亂,保留鍵名 | shuffle(array(“a”=> “Dog”)) | Array(“a”=> “Dog”) |
count | 計算個數 | count(array(“Tom”)) | |
array_flip | 鍵值反轉 | array_flip(array(“Tom”, “Jack”)) | Array([Tom]=>0 [Jack]=>1) |
array_keys | 傳回所有鍵 | array_keys(array(“Tom”, “Jack”)) | Array([0]=>0 [1]=>1) |
array_values | 傳回所有值 | array_values(array(“Tom”, “Jack”)) | Array([0]=>Tom [1]=>Jack) |
array_reverse | 元素順序反轉 | array_reverse(array(“Tom”, “Jack”)) | Array([0]=>Jack [1]=>Tom) |
array_count_values | 統計值次數 | array_count_values(array(“Tom”, “Jack”)) | Array([Tom]=>1 [Jack]=>1) |
array_rand | 随機取出鍵 | array_rand(array(“Tom”, “Jack”)) | |
array_unique | 删除重複值,傳回剩餘數組 | array_unique(array(“Tom”, “Jack”)) | Array(“Tom”, “Jack”) |
5、數組排序
sort | 升序值排序,不保留鍵名 | sort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | |
rsort | 逆向排序,不保留鍵名 | rsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | |
asort | 升序排序,保持索引關系 | asort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | Array([Cat]=>Jack [Dog]=>Tom) |
arsort | 對數組逆向排序,保持索引關系 | arsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | Array([Dog]=>Tom [Cat]=>Jack) |
ksort | 鍵名排序 | ksort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | |
krsort | 逆向排序 | krsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | |
natsort | 自然順序排序 | natsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) | |
natcasesort | 自然排序,不區分大小寫 | natcasesort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) |
四、檔案系統函數
39個
1、檔案屬性
9個
file_exists | 檢查檔案或目錄是否存在 | file_exists(“demo.txt”) | |
filesize | 檔案大小byte | filesize(“demo.txt”) | 18 |
is_readable | 檔案是否可讀 | is_readable(“demo.txt”) | |
is_writable | 檔案是否可寫 | is_writable(“demo.txt”) | |
is_executable | 檔案是否可執行 | is_executable(“demo.txt”) | |
filectime | 檔案建立時間 | filectime(“demo.txt”) | 1552228956 |
filemtime | 檔案的修改時間 | filemtime(“demo.txt”) | |
fileatime | 上次通路時間 | fileatime(“demo.txt”) | |
stat | 檔案屬性 | stat(“demo.txt”) | Array |
2、檔案操作
16個
fopen | 打開檔案或者 URL | fopen(“demo.txt”, “r”) | $handle |
fclose | 關閉檔案指針 | fclose($handle) | true/false |
fwrite | 寫入檔案 | fwrite($handle, $data) | - |
fputs | 寫入檔案(同上) | ||
feof | 檔案指針是否到了檔案結束的位置 | feof($handle) | |
fgets | 從檔案指針中讀取一行 | fgets($handle) | $line |
fgetc | 從檔案指針中讀取字元 | fgetc($handle) | $char |
file | 讀入整個檔案到數組 | file(“demo.txt”) | $lines |
readfile | 讀入一行 | readfile(“demo.txt”) | |
file_get_contents | 整個檔案讀入一個字元串 | file_get_contents(“demo.txt”) | $content |
file_put_contents | 将字元串寫入檔案 | file_put_contents(“demo.txt”, $data) | |
ftell | 傳回檔案指針讀/寫的位置 | ||
fseek | 在檔案指針中定位 | ||
rewind | 倒回檔案指針的位置 | ||
flock | 輕便的執行檔案鎖定 | ||
fread | 讀取字元 | fread($handle, 3) | $chars |
3、目錄
12個
basename | 傳回檔案名 | basename(“root/demo.txt”) | demo.txt |
dirname | 傳回目錄 | dirname(“root/demo.txt”) | root |
pathinfo | 檔案資訊 | pathinfo(“root/demo.txt”) | Array([dirname] => root [basename] => demo.txt [extension] => txt [filename] => demo) |
opendir | 打開目錄句柄 | opendir(’/’) | $dir_handle |
readdir | 從目錄句柄中讀取條目 | readdir($dir_handle) | |
closedir | 關閉目錄句柄 | closedir($dir_handle) | |
rewinddir | 目錄流重置到目錄的開頭 | rewinddir($dir_handle) | |
mkdir | 建立目錄 | ||
rmdir | 删除目錄 | ||
unlink | 删除檔案 | ||
copy | 拷貝檔案 | ||
rename | 重命名一個檔案或目錄 |
4、檔案的上傳與下載下傳
2個
is_uploaded_file | 判斷檔案是否是通過 HTTP POST上傳的 | ||
move_uploaded_file | 将上傳的檔案移動到新位置 |
五、時間
time | 時間戳 | time() | 1552231653 |
mktime | 取得時間戳 | mktime(0, 0, 0, 4, 25, 2012) | 1335283200 |
date | 時間格式化 | date(‘Y年m月d日 H:i:s’) | 2019年03月10日 23:28:12 |
checkdate | 驗證一個格裡高裡日期 | ||
date_default_timezone_set | 設定預設時區 | ||
getdate | 取得日期/時間資訊 | getdate() | |
strtotime | 将任何英文文本的日期時間描述解析為 Unix 時間戳 | strtotime(“last Monday”) | 1551628800 |
microtime | 傳回目前 Unix 時間戳和微秒數 | microtime() | 0.64341300 1552232106 |
sleep | 休息 | sleep(3) |
六、其他函數
3個
intval | 擷取變量的整數值 | intval(“1111”, 2); intval(“20”) | 15 ; 20 |
isset | 檢測變量是否設定 | isset($hi) | false |
empty | 檢查一個變量是否為空 | empty($hi) | true |
json_encode | 對變量進行JSON編碼 | json_encode([‘name’=>‘Tom’]) | {“name”:“Tom”} |
json_decode | JSON解碼傳回對象 | json_decode(’{“name”:“Tom”}’)) | object(stdClass){[“name”]=>“Tom”} |
JSON解碼傳回數組 | json_decode(’{“name”:“Tom”}’, true) | array(1) {[“name”]=>“Tom”} |
參考:
Php 常用内置函數總結