php怎么读取ini配置文件

PHP
2486
2023/11/8 3:39:07
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在PHP中,可以使用parse_ini_file()函数来读取INI配置文件。

以下是使用parse_ini_file()函数读取INI配置文件的示例代码:

$config = parse_ini_file('config.ini');

// 读取配置文件中的值
$host = $config['host'];
$username = $config['username'];
$password = $config['password'];

// 输出配置文件的值
echo "Host: $host\n";
echo "Username: $username\n";
echo "Password: $password\n";

在上面的示例代码中,parse_ini_file()函数接受一个INI配置文件的路径作为参数,并返回一个包含配置文件内容的关联数组。然后,可以像访问普通的关联数组一样,通过键名来访问配置文件中的值。

假设config.ini文件的内容如下:

host=localhost
username=admin
password=123456

运行上面的示例代码,将输出以下内容:

Host: localhost
Username: admin
Password: 123456

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 如何自定义php trim()行为