PHP函数大全:必会函数列表
PHP是一种广泛使用的脚本语言,特别适用于Web开发。PHP有非常强大的函数库,这些函数涵盖了各种需求,包括字符串处理、日期/时间处理、文件处理、MySQL数据库等等。以下是必会函数列表,每个函数都详细介绍了其参数和用法。
1. echo
echo输出一个或多个字符串。
语法:echo string1, string2, ...
参数:string1, string2 等表达式
示例:
echo "Hello world!";
echo "This ","is ","a ","test!";
2. print
print输出一个字符串。
语法:print expression;
参数:expression
示例:
print "Hello world!";
print "This is a test!";
3. printf
printf格式化输出字符串。
语法:printf(format, argument1, argument2, ...)
参数:format 表示字符串格式,argument1, argument2 等是要输出的值。
示例:
printf("The result is %d", 123);
4. strlen
strlen获取字符串长度。
语法:strlen(string)
参数:string 表示要获取长度的字符串。
示例:
echo strlen("Hello World"); //输出11
5. strpos
strpos查找字符串中是否包含某个子字符串,并返回其位置。
语法:strpos(string, find, start)
参数:string 表示要搜索的字符串,find 表示要查找的子字符串,start 表示开始搜索的位置。
示例:
echo strpos("Hello World", "o"); //输出4
6. substr
substr从字符串中获取子字符串。
语法:substr(string, start, length)
参数:string 表示要获取子字符串的字符串,start 表示要获取子字符串的开始位置,length表示要获取子字符串的长度。
示例:
echo substr("Hello World", 1, 3); //输出"ell"
7. strtolower
strtolower将字符串转换为小写。
语法:strtolower(string)
参数:string 表示要转换为小写的字符串。
示例:
echo strtolower("Hello World"); //输出"hello world"
8. strtoupper
strtoupper将字符串转换为大写。
语法:strtoupper(string)
参数:string 表示要转换为大写的字符串。
示例:
echo strtoupper("Hello World"); //输出"HELLO WORLD"
9. str_replace
str_replace替换字符串中的字符或字符串。
语法:str_replace(search, replace, subject)
参数:search表示要查找的字符或字符串,replace表示要替换的字符或字符串,subject表示要替换的字符串。
示例:
echo str_replace("world", "Dolly", "Hello world!"); //输出"Hello Dolly!"
10. explode
explode将字符串分割成数组。
语法:explode(separator, string)
参数:separator表示要用来分割字符串的字符或字符串,string表示要分割的字符串。
示例:
$str = "Hello world. It's a beautiful day.";
$arr = explode(" ", $str);
print_r($arr);
输出:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
11. implode
implode将数组元素组合成字符串。
语法:implode(glue, pieces)
参数:glue表示单元之间连接的字符串,pieces表示要连接的数组。
示例:
$arr = array('Hello', 'world!', 'It\'s', 'a', 'beautiful', 'day.');
$str = implode(" ", $arr);
echo $str;
12. date
date获取当前日期和时间。
语法:date(format, timestamp)
参数:format表示日期和时间的格式,timestamp表示时间戳。
示例:
echo date("Y/m/d"); //输出当前日期
13. time
time获取当前时间戳。
语法:time()
参数:无
示例:
echo time(); //输出当前时间戳
14. mktime
mktime获取指定日期的时间戳。
语法:mktime(hour, minute, second, month, day, year)
参数:hour表示小时,minute表示分钟,second表示秒,month表示月份,day表示日期,year表示年份。
示例:
echo mktime(0, 0, 0, 12, 31, 2017); //输出指定日期的时间戳
15. file_get_contents
file_get_contents读取文件内容。
语法:file_get_contents(filename)
参数:filename文件名
示例:
echo file_get_contents("test.txt"); //输出test.txt文件内容
16. file_put_contents
file_put_contents写入文件内容。
语法:file_put_contents(filename, data)
参数:filename文件名,data要写入的内容。
示例:
file_put_contents("test.txt", "Hello World"); //将"Hello World"写入test.txt文件内
17. fopen
fopen打开文件。
语法:fopen(filename, mode)
参数:filename文件名,mode打开方式。
示例:
$fh = fopen("test.txt", "r");
18. fclose
fclose关闭文件。
语法:fclose(handle)
参数:handle文件句柄。
示例:
fclose($fh);
19. fread
fread读取文件内容。
语法:fread(handle, length)
参数:handle文件句柄,length读取的长度。
示例:
echo fread($fh, filesize("test.txt"));
20. fwrite
fwrite写入文件内容。
语法:fwrite(handle, string)
参数:handle文件句柄,string要写入的内容。
示例:
fwrite($fh, "Hello World");
21. fclose
fclose关闭文件。
语法:fclose(handle)
参数:handle文件句柄。
示例:
fclose($fh);
22. mysql_connect
mysql_connect连接MySQL服务器。
语法:mysql_connect(server, username, password)
参数:server服务器名称,username用户名,password密码。
示例:
$link = mysql_connect('localhost', 'username', 'password');
23. mysql_select_db
mysql_select_db选择数据库。
语法:mysql_select_db(database_name)
参数:database_name数据库名。
示例:
mysql_select_db('database_name');
24. mysql_query
mysql_query执行MySQL查询。
语法:mysql_query(query, link_identifier)
参数:query查询语句,link_identifier服务器链接。
示例:
$result = mysql_query('SELECT * FROM table_name');
25. mysql_fetch_array
mysql_fetch_array从结果集中获取行。
语法:mysql_fetch_array(result, result_type)
参数:result查询结果集,result_type返回结果的类型。
示例:
$result = mysql_query("SELECT id,name FROM table_name");
while($row = mysql_fetch_array($result))
{
echo $row['id'] . " " . $row['name'] . "<br />";
}
26. mysql_close
mysql_close关闭MySQL连接。
语法:mysql_close(link_identifier)
参数:link_identifier连接标识符。
示例:
mysql_close($link);
27. count
count计算数组元素的个数。
语法:count(array, mode)
参数:array要计算的数组,mode计算模式。
示例:
$arr=array(1,2,3,4,5);
echo count($arr); //输出5
28. sort
sort对数组进行升序排列。
语法:sort(array, sort_flags)
参数:array要排序的数组,sort_flags排序方式。
示例:
$arr=array("Horse","Dog","Cat","Ant");
sort($arr);
print_r($arr);
29. rsort
rsort对数组进行降序排列。
语法:rsort(array, sort_flags)
参数:array要排序的数组,sort_flags排序方式。
示例:
$arr=array("Horse","Dog","Cat","Ant");
rsort($arr);
print_r($arr);
30. in_array
in_array在数组中查找一个值是否存在。
语法:in_array(needle, haystack, strict)
参数:needle要查找的值,haystack要搜索的数组,strict是否严格。
示例:
$arr=array("Horse","Dog","Cat","Ant");
if (in_array("Dog", $arr))
{
echo "Dog is in the array";
}
31. array_search
array_search获取一个值在数组中的位置。
语法:array_search(needle, haystack, strict)
参数:needle要查找的值,haystack要搜索的数组,strict是否严格比较。
示例:
$arr=array("Horse","Dog","Cat","Ant");
echo array_search("Dog", $arr); //输出1
32. array_push
array_push向数组末尾添加一个或多个元素。
语法:array_push(array, value1, value2...)
参数:array要添加元素的数组,value1, value2等是要添加的值。
示例:
$arr=array("Horse","Dog","Cat
