The parse_ini_file Function
The parse_ini_file
function takes the path to an INI file as the first parameter,
as well as two optional parameters: the second parameter determines whether sections should be processed,
and the third - the file scanning mode.
Syntax
parse_ini_file(
string $filename,
bool $process_sections = false,
int $scanner_mode = INI_SCANNER_NORMAL
);
Example
Let's create a config.ini file with the following content:
; config.ini
database = mysql
host = localhost
user = root
password = 12345
Now let's read it:
<?php
$res = parse_ini_file('config.ini');
print_r($res);
?>
Code execution result:
[
'database' => 'mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '12345'
]
Example
Now let's add sections to our INI file:
; config.ini
[database]
type = mysql
host = localhost
[credentials]
user = root
password = 12345
Let's read the file with section processing:
<?php
$res = parse_ini_file('config.ini', true);
print_r($res);
?>
Code execution result:
[
'database' => [
'type' => 'mysql',
'host' => 'localhost'
],
'credentials' => [
'user' => 'root',
'password' => '12345'
]
]
Example
Demonstrating work with INI_SCANNER_TYPED:
; config.ini
debug = true
port = 3306
timeout = 3.5
Let's read the file with value typing:
<?php
$res = parse_ini_file('config.ini', false, INI_SCANNER_TYPED);
print_r($res);
?>
Code execution result:
[
'debug' => true,
'port' => 3306,
'timeout' => 3.5
]
See Also
-
the
file_get_contents
function,
which reads a file into a string -
the
file
function,
which reads a file into an array