Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

parse_ini_file

Reads a settings file in ini format.

Syntax

parse_ini_file(string $filename, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL): array|false

Parameters

ParameterTypeDescription
$filenamestringThe path to the file.
$process_sectionsboolOptional. Honour `[name]` sections.
$scanner_modeintOptional. `INI_SCANNER_TYPED` converts numbers and booleans.

Return value

array|falseAn array of settings, or `false`.

Examples

Settings with sections and types:

<?php
 
$file = sys_get_temp_dir() . '/ref-settings.ini';
file_put_contents($file, "debug = true\n\n[db]\nhost = localhost\nport = 3306\n");
 
$config = parse_ini_file($file, true, INI_SCANNER_TYPED);
 
var_dump($config['debug'], $config['db']['port']);

Output:

bool(true)
int(3306)

Notes

Without INI_SCANNER_TYPED every value is a string, and true becomes '1'. parse_ini_string() parses a string instead of a file.

See also

  • getenv — Returns the value of an environment variable.
  • json_decode — Parses a JSON string.
  • file — Reads a file into an array of lines.

Related recipes

In the tutorial

  • Configuration and environment — Where to keep the settings of a PHP project — a configuration file, environment variables, the .env file, different modes for development and the server.

Thanks for reading!

Donate to the site — we work hard for you.