一、字符串处理

  1. strtoupper() - 转换为大写

    1. echo strtoupper('hello'); // 输出 HELLO
  2. strtolower() - 转换为小写

    1. echo strtolower('HELLO'); // 输出 hello
  3. trim() - 去除首尾空格/特殊字符

    1. echo trim(' text '); // 输出 "text"
  4. substr() - 截取字符串

    1. echo substr('Hello World', 0, 5); // 输出 Hello
  5. strlen() - 获取字符串长度

    1. echo strlen('Hello'); // 输出 5
  6. strpos() - 查找子字符串位置

    1. echo strpos('Hello World', 'World'); // 输出 6
  7. str_replace() - 替换字符串

    1. echo str_replace('World', 'PHP', 'Hello World'); // 输出 Hello PHP

二、数组操作

  1. array_merge() - 合并数组

    1. print_r(array_merge([1,2], [3,4])); // 输出 [1,2,3,4]
  2. array_push() - 向数组尾部添加元素

    1. $arr = [1];
    2. array_push($arr, 2);
    3. print_r($arr); // 输出 [1,2]
  3. array_pop() - 弹出数组最后一个元素

    1. $arr = [1,2];
    2. echo array_pop($arr); // 输出 2
  4. in_array() - 检查值是否存在

    1. var_dump(in_array(2, [1,2])); // 输出 bool(true)
  5. array_keys() - 获取所有键名

    1. print_r(array_keys(['a' => 1, 'b' => 2])); // 输出 ['a', 'b']
  6. shuffle() - 打乱数组顺序

    1. $arr = [1,2,3];
    2. shuffle($arr);
    3. print_r($arr);

三、日期与时间

  1. date() - 格式化日期

    1. echo date('Y-m-d H:i:s'); // 输出当前时间,如 2023-10-05 14:30:00
  2. time() - 获取当前时间戳

    1. echo time(); // 输出秒数,如 1696505400
  3. strtotime() - 转换时间字符串为时间戳

    1. echo strtotime('next Monday'); // 输出下周一的时间戳
  4. date_create() - 创建 DateTime 对象

    1. $date = date_create('2023-10-05');
    2. echo date_format($date, 'Y/m/d'); // 输出 2023/10/05

四、文件与目录

  1. file_get_contents() - 读取文件内容

    1. $content = file_get_contents('file.txt');
  2. file_put_contents() - 写入文件

    1. file_put_contents('file.txt', 'Hello World');
  3. fopen() / fwrite() - 打开并写入文件

    1. $file = fopen('file.txt', 'w');
    2. fwrite($file, 'Hello');
    3. fclose($file);
  4. unlink() - 删除文件

    1. unlink('file.txt');
  5. scandir() - 列出目录内容

    1. print_r(scandir('.'));

五、数学函数

  1. rand() - 生成随机数

    1. echo rand(1, 10); // 输出 1~10 之间的整数
  2. mt_rand() - 更高效的随机数

    1. echo mt_rand(1, 100);
  3. number_format() - 格式化数字

    1. echo number_format(1234.56, 2); // 输出 1,234.56
  4. round() - 四舍五入

    1. echo round(3.1415, 2); // 输出 3.14

六、正则表达式

  1. preg_match() - 匹配正则表达式

    1. preg_match('/\d+/', 'abc123', $matches);
    2. echo $matches[0]; // 输出 123
  2. preg_replace() - 正则替换

    1. echo preg_replace('/\s+/', '', 'Hello World'); // 输出 HelloWorld

七、会话与Cookie

  1. session_start() - 开启会话

    1. session_start();
    2. $_SESSION['user'] = 'John';
  2. setcookie() - 设置Cookie

    1. setcookie('user', 'John', time() + 3600);

八、数据库操作(以 MySQLi 为例)

  1. mysqli_connect() - 连接数据库

    1. $conn = mysqli_connect('localhost', 'user', 'password', 'dbname');
  2. mysqli_query() - 执行 SQL 查询

    1. $result = mysqli_query($conn, 'SELECT * FROM users');

九、图像处理

  1. imagecreate() - 创建图像资源

    1. $image = imagecreate(100, 100);
  2. imagejpeg() - 输出 JPEG 图像

    1. header('Content-Type: image/jpeg');
    2. imagejpeg($image);

十、网络相关

  1. gethostbyname() - 解析域名

    1. echo gethostbyname('www.google.com');
  2. ip2long() - IP 转整数

    1. echo ip2long('127.0.0.1');

十一、加密与安全

  1. md5() - MD5 加密

    1. echo md5('password');
  2. password_hash() - 安全哈希

    1. echo password_hash('password', PASSWORD_DEFAULT);

十二、类型转换

  1. 强制类型转换
    1. $num = (int)'123'; // 转换为整数
    2. $str = strval(123); // 转换为字符串

十三、变量处理

  1. isset() - 检查变量是否存在

    1. var_dump(isset($undefinedVar)); // 输出 bool(false)
  2. empty() - 检查变量是否为空

    1. var_dump(empty('')); // 输出 bool(true)

十四、输出控制

  1. ob_start() - 开启输出缓冲
    1. ob_start();
    2. echo 'Hello';
    3. $content = ob_get_clean();

十五、错误处理

  1. error_reporting() - 设置错误报告级别
    1. error_reporting(E_ALL);

以上仅为常用函数示例,PHP 内置函数库非常丰富,具体可参考官方文档:PHP Functions。使用时需注意函数参数和返回值类型,以及版本兼容性。