php怎么讀取ini配置文件

PHP
小億
178
2023-11-24 03:39:07
欄目: 編程語言

在PHP中,可以使用parse_ini_file()函數(shù)來讀取INI配置文件。

以下是使用parse_ini_file()函數(shù)讀取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()函數(shù)接受一個(gè)INI配置文件的路徑作為參數(shù),并返回一個(gè)包含配置文件內(nèi)容的關(guān)聯(lián)數(shù)組。然后,可以像訪問普通的關(guān)聯(lián)數(shù)組一樣,通過鍵名來訪問配置文件中的值。

假設(shè)config.ini文件的內(nèi)容如下:

host=localhost
username=admin
password=123456

運(yùn)行上面的示例代碼,將輸出以下內(nèi)容:

Host: localhost
Username: admin
Password: 123456

0