欢迎访问宙启技术站
智能推送

PHP内置函数详解:常用函数一网打尽

发布时间:2023-06-21 15:04:16

PHP是当今最流行的Web开发语言之一,它内置了很多实用的函数,可以大大提高开发效率。本文将详细介绍PHP中常用函数的用法和实例,让你迅速掌握其使用方法。

一、字符串处理函数

1. strlen()函数:计算字符串长度

语法:int strlen(string $string)

实例:

$a = "hello world";

echo strlen($a); //输出11

2. strtoupper()函数:将字符串转换为大写字母

语法:string strtoupper(string $string)

实例:

$a = "hello world";

echo strtoupper($a); //输出HELLO WORLD

3. strtolower()函数:将字符串转换为小写字母

语法:string strtolower(string $string)

实例:

$a = "HELLO WORLD";

echo strtolower($a); //输出hello world

4. trim()函数:去除字符串两侧的空格

语法:string trim(string $string)

实例:

$a = "     hello world    ";

echo trim($a); //输出hello world

5. substr()函数:截取字符串

语法:string substr(string $string, int $start[, int $length])

实例:

$a = "hello world";

echo substr($a, 0, 5); //输出hello

二、数学函数

1. abs()函数:计算绝对值

语法:float abs(float $number)

实例:

$a = -10;

echo abs($a); //输出10

2. ceil()函数:向上取整

语法:float ceil(float $number)

实例:

$a = 2.5;

echo ceil($a); //输出3

3. floor()函数:向下取整

语法:float floor(float $number)

实例:

$a = 2.9;

echo floor($a); //输出2

4. rand()函数:生成随机数

语法:int rand(int $min, int $max)

实例:

echo rand(1, 100); //输出1-100之间的随机数

三、日期时间函数

1. date()函数:格式化日期和时间

语法:string date(string $format[, int $timestamp])

实例:

echo date("Y-m-d H:i:s"); //输出当前的年月日时分秒

2. time()函数:获取当前时间戳

语法:int time()

实例:

echo time(); //输出当前时间的时间戳

3. mktime()函数:获取指定时间的时间戳

语法:int mktime(int $hour, int $minute, int $second, int $month, int $day, int $year)

实例:

echo mktime(0, 0, 0, 12, 31, 2021); //输出12月31日2021年的时间戳

四、数组函数

1. array()函数:创建数组

语法:array array([mixed $...])

实例:

$arr = array("apple", "banana", "orange");

print_r($arr); //输出Array([0] => apple [1] => banana [2] => orange)

2. count()函数:计算数组中元素个数

语法:int count(array $array[, int $mode])

实例:

$arr = array("apple", "banana", "orange");

echo count($arr); //输出3

3. array_push()函数:向数组尾部添加一个或多个元素

语法:int array_push(array &$array, mixed $value1[, mixed $...])

实例:

$arr = array("apple", "banana", "orange");

array_push($arr, "grape");

print_r($arr); //输出Array([0] => apple [1] => banana [2] => orange [3] => grape)

4. array_pop()函数:弹出数组最后一个元素

语法:mixed array_pop(array &$array)

实例:

$arr = array("apple", "banana", "orange");

array_pop($arr);

print_r($arr); //输出Array([0] => apple [1] => banana)

五、文件操作函数

1. fopen()函数:打开文件

语法:resource fopen(string $filename, string $mode)

实例:

$fp = fopen("file.txt", "w+");

fclose($fp);

2. fwrite()函数:写入文件

语法:int fwrite(resource $handle, string $string[, int $length])

实例:

$fp = fopen("file.txt", "w+");

fwrite($fp, "hello world");

fclose($fp);

3. fread()函数:读取文件

语法:string fread(resource $handle, int $length)

实例:

$fp = fopen("file.txt", "r");

echo fread($fp, filesize("file.txt"));

fclose($fp);

4. file_get_contents()函数:读取文件内容到字符串中

语法:string file_get_contents(string $filename)

实例:

echo file_get_contents("file.txt");

六、MySQL函数

1. mysqli_connect()函数:连接MySQL数据库

语法:resource mysqli_connect(string $host, string $username, string $password[, string $database[, int $port[, string $socket]]])

实例:

$con = mysqli_connect("localhost", "root", "123456", "test");

2. mysqli_query()函数:执行SQL语句

语法:mixed mysqli_query(resource $con, string $query[, int $resultmode])

实例:

$con = mysqli_connect("localhost", "root", "123456", "test");

$result = mysqli_query($con, "SELECT * FROM user");

3. mysqli_fetch_array()函数:从查询结果中获取一行作为关联数组或数字数组

语法:array mysqli_fetch_array(resource $result[, int $resulttype])

实例:

$con = mysqli_connect("localhost", "root", "123456", "test");

$result = mysqli_query($con, "SELECT * FROM user");

while($row = mysqli_fetch_array($result)){

    echo $row['id'] . ' ' . $row['name'] . ' ' . $row['age'] . '<br>';

}

以上就是常用的PHP内置函数介绍,当然PHP内置函数还有很多,如正则表达式函数、文件上传函数等等,需要使用时可自行查阅PHP官方文档。熟练掌握内置函数可以极大提高开发效率,有助于编写更加优雅和高效的代码。