共计整理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”) hello
strtoupper 字符串转为大写 strtoupper(“hello”) HELLO
ucfirst 字符串首字母大写 ucfirst(“hello world”) Hello world
ucwords 字符串所有首字符大写 ucwords(“hello world”) Hello World
wordwrap 字符串折行 wordwrap(“hello world”, 5) hello\nworld

2、字符串网页相关

10个

函数 说明 举例 结果
parse_str 字符串解析成变量 parse_str(“id=23&name=Tom”, $list) Array([id] => 23 [name] => Tom)
htmlentities 字符转为HTML实体 htmlentities(“hello&”) hello&
htmlspecialchars 预定义字符转html编码 htmlspecialchars(“hello&”) hello&
nl2br \n转义为
标签
nl2br(“hello\nworld”) hello
\nworld
strip_tags 剥去 HTML、XML 以及 PHP 的标签 strip_tags(“Hello world!”) Hello world!
addcslashes 指定字符前添加反斜线 addcslashes(“Hello number”, “n”) Hello \number
stripcslashes 删除反斜线 stripcslashes(“Hello \nu\mber”) Hello \number
addslashes 预定义字符前添加反斜线 addslashes(“Hello’ number”) Hello’ number
stripslashes 删除预定义字符前的转义字符 stripslashes(“Hello’ number”) Hello’ number
quotemeta 预定义的字符前添加反斜线 quotemeta(“Hello world.”) Hello world.
rawurldecode 解码URL编码的字符串 rawurldecode(‘my%20name’) my name
rawurlencode 字符串进行URL编码 rawurlencode(‘my name’) my%20name

3、字符串比较

8个

函数 说明 举例 结果
chr ASCII 值返回字符 chr(65) A
ord 字符串第一个字符的ASCII值 ord(“hello”) 104
strcasecmp 不区分大小写比较两字符串 strcasecmp(“Hello”, “HELLO”) 0
strcmp 区分大小写比较两字符串 strcmp(“Hello”, “HELLO”) 32
strncmp 比较字符串前n个字符,区分大小写 strncmp(“Hello”, “HELLO”, 3) 32
strncasecmp 比较字符串前n个字符,不区分大小写 strncasecmp(“Hello”, “HELLO”, 3) 0
strnatcmp 自然顺序法比较字符串长度,区分大小写 (“Hello”, “HELLO”) 1
strnatcasecmp 自然顺序法比较字符串长度, 不区分大小写 strnatcasecmp(“Hello”, “HELLO”) 0

4、字符串切割与拼接

6个

函数 说明 举例 结果
chunk_split 将字符串分成小块 chunk_split(“hello world”, 3) hel lo wor ld
strtok 切开字符串 strtok(“hello world”, " ") hello
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”) xxello
substr_replace 字符串替换 substr_replace(“hello, hello”, “x”, 1, 6) hxhello
similar_text 两字符串相同字符的数量 similar_text(“hello”, “Hello”) 5
strstr 字符串开始位置到结束的字符串 strstr(“HELLO hello”, “l”) llo
strchr strstr()的别名 strchr(“HELLO hello”, “l”) llo
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”) 2
strrpos 字符最后出现的位置 strrpos(“hello,HELLO”, “l”) 3
strripos 字符最后出现的位置,不区分大小写 strripos(“hello, HELLO”, “l”) 10
strspn 字符串中首次符合mask的长度 strspn(“www.baidu.com”, “ad”) 0
strcspn 字符串中不符合mask的长度 strcspn(“www.baidu.com”, “ad”) 5

6、字符串统计

4个

函数 说明 举例 结果
substr_count 统计字符串出现次数 substr_count(“hello”, “l”) 2
str_word_count 统计含有的单词数 str_word_count(“hello world”) 2
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、数组合并和拆分

5个

函数 说明 举例 结果
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、数组查找替换

5个

函数 说明 举例 结果
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”)) 1
array_key_exists 查找键 array_key_exists(“b”, array(“b”=>“Cat”)) 1

4、数组元素操作

10个

函数 说明 举例 结果
key 指针当前指向的键名 key(array(“a”=>“Dog”,“b”=>“Cat”)) a
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”)) Cat
reset 指向第一个元素 reset(array(“a”=>“Dog”,“b”=>“Cat”)) Dog
list 序列解包 list( a , a, a,b)=array(“Dog”,“Cat”) $a=“Dog”; $b=“Cat”
array_shift 返回删除的第一个元素 array_shift(array(“Dog”,“Cat”)) Dog
array_unshift 开头插入元素 array_unshift(&array(),“Dog”) Array([0]=>“Dog”)
array_push 最后压入元素 array_push(&array(), “Dog”) Array([0]=>“Dog”)
array_pop 弹出最后一个元素 array_pop(&array(), “Dog”) Dog
shuffle 数组打乱,保留键名 shuffle(array(“a”=> “Dog”)) Array(“a”=> “Dog”)
count 计算个数 count(array(“Tom”)) 1
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”)) 1
array_unique 删除重复值,返回剩余数组 array_unique(array(“Tom”, “Jack”)) Array(“Tom”, “Jack”)

5、数组排序

8个

函数 说明 举例 结果
sort 升序值排序,不保留键名 sort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) Array([0]=>Jack [1]=>Tom)
rsort 逆向排序,不保留键名 rsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) Array([0]=>Tom [1]=>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”)) Array([Cat]=>Jack [Dog]=>Tom)
krsort 逆向排序 krsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) Array([Dog]=>Tom [Cat]=>Jack)
natsort 自然顺序排序 natsort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) Array([Cat]=>Jack [Dog]=>Tom)
natcasesort 自然排序,不区分大小写 natcasesort(array(“Dog”=>“Tom”, “Cat”=>“Jack”)) Array([Cat]=>Jack [Dog]=>Tom)
四、文件系统函数

39个

1、文件属性

9个

函数 说明 举例 结果
file_exists 检查文件或目录是否存在 file_exists(“demo.txt”) 1
filesize 文件大小byte filesize(“demo.txt”) 18
is_readable 文件是否可读 is_readable(“demo.txt”) 1
is_writable 文件是否可写 is_writable(“demo.txt”) 1
is_executable 文件是否可执行 is_executable(“demo.txt”) 1
filectime 文件创建时间 filectime(“demo.txt”) 1552228956
filemtime 文件的修改时间 filemtime(“demo.txt”) 1552228956
fileatime 上次访问时间 fileatime(“demo.txt”) 1552228956
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) 1
fgets 从文件指针中读取一行 fgets($handle) $line
fgetc 从文件指针中读取字符 fgetc($handle) $char
file 读入整个文件到数组 file(“demo.txt”) $lines
readfile 读入一行 readfile(“demo.txt”) $line
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 将上传的文件移动到新位置
五、时间

9个

函数 说明 举例 结果
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() Array
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_decode JSON解码返回数组 json_decode(’{“name”:“Tom”}’, true) array(1) {[“name”]=>“Tom”}

参考:
Php 常用内置函数总结