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

phpparse_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:

php; config.ini
database = mysql
host = localhost
user = root
password = 12345

Now let's read it:

php<?php
  $res = parse_ini_file('config.ini');
  print_r($res);
?>

Code execution result:

php[
  'database' => 'mysql',
  'host' => 'localhost',
  'user' => 'root',
  'password' => '12345'
]

Example

Now let's add sections to our INI file:

php; config.ini
[database]
type = mysql
host = localhost

[credentials]
user = root
password = 12345

Let's read the file with section processing:

php<?php
  $res = parse_ini_file('config.ini', true);
  print_r($res);
?>

Code execution result:

php[
  'database' => [
    'type' => 'mysql',
    'host' => 'localhost'
  ],
  'credentials' => [
    'user' => 'root',
    'password' => '12345'
  ]
]

Example

Demonstrating work with INI_SCANNER_TYPED:

php; config.ini
debug = true
port = 3306
timeout = 3.5

Let's read the file with value typing:

php<?php
  $res = parse_ini_file('config.ini', false, INI_SCANNER_TYPED);
  print_r($res);
?>

Code execution result:

php[
  '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