在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