本文目录导读:

PHP 官方在不同版本中弃用了许多旧函数,以下是主要版本中弃用或移除的关键函数列表:
PHP 7.x 弃用的主要函数
PHP 7.0
mysql_*系列函数(如mysql_connect,mysql_query等)mcrypt_*系列函数ereg/ereg_replace正则函数split函数set_socket_blocking函数call_user_method/call_user_method_array
PHP 7.1
mcrypt_*函数(正式弃用)setlocale的常量 INTL 相关
PHP 7.2
create_function__autoloadeach函数assert()字符串模式mbstring.func_overload设置
PHP 7.3
define()在__autoload中不推荐image2wbmp函数mbregex编码模式
PHP 7.4
real类型转换money_format函数get_magic_quotes_gpc/get_magic_quotes_runtimeimplode()参数顺序反转(real)类型转换
PHP 8.x 弃用或移除的函数
PHP 8.0(已移除)
create_function(完全移除)each(完全移除)fgetss函数gd_info()部分常量get_magic_quotes_*系列money_formatldap_*部分函数socket_*部分函数
PHP 8.1
mysqli_report()模式改变strftime/gmstrftimedate_sunrise/date_sunset的旧签名mhash*函数
PHP 8.2
utf8_encode/utf8_decodemb_str_split的函数签名变更locale_get_display_variantsnmp*部分函数
PHP 8.3(最新弃用)
random_int的true参数变体ldap_exop的部分用法
推荐替代方案
| 弃用函数 | 推荐替代 |
|---|---|
mysql_* |
mysqli_* 或 PDO |
mcrypt_* |
OpenSSL 或 Sodium |
ereg / split |
preg_match / preg_split |
create_function |
匿名函数(Closure) |
each |
foreach 循环 |
utf8_encode |
mb_convert_encoding |
strftime |
date 或 IntlDateFormatter |
money_format |
NumberFormatter |
检查代码中的弃用函数
可以使用以下工具或方法检查:
# 使用 Composer 工具 composer require --dev dsa/php-deprecation-fixer # 使用 PHPStan 静态分析 vendor/bin/phpstan analyse --level=max src/
建议定期检查 PHP 官方文档和 RFC,了解最新的弃用情况和迁移建议,对于遗留代码,应优先考虑替换这些弃用函数,以确保代码的安全性和可维护性。