parse_ini_string

Parses a string in php.ini format and returns its settings as an array.

Signature
parse_ini_string(string $ini_string, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL): array|false
  • Added in PHP 5.3.0

Parameters

ParameterTypeDescription
$ini_stringstringThe ini-formatted text to parse.
$process_sectionsoptionalbool= falseWhen true, the result is nested one level deep, keyed by [section] name.
$scanner_modeoptionalint= INI_SCANNER_NORMALINI_SCANNER_NORMAL, INI_SCANNER_RAW (no interpolation or escaping) or INI_SCANNER_TYPED (bool/int/null preserved).

Return value

array|falseAn associative array of settings on success, or false on a parse failure.

Errors

Emits an E_WARNING and returns false when the string contains a syntax error.

Changelog

  • PHP 5.6.1INI_SCANNER_TYPED was added, so booleans, integers and null keep their native types instead of becoming strings.

parse_ini_string() is parse_ini_file() with the disk access removed: you hand it the ini text and it hands back the array. Everything about the syntax, the section handling and the scanner modes is identical.

When to reach for it

It shines whenever the configuration does not live in a local file. Common cases: an ini blob stored in a database column or environment variable, a config fragment pulled from an object store or secrets manager, a heredoc of defaults you merge under user settings, and unit tests where writing a temp file for each fixture would be noise.

It is also the safe way to parse remote content. Rather than pointing parse_ini_file() at a URL, fetch the body with your HTTP client, inspect or size-limit it, then parse the string you actually received.

What trips people up

The same reserved words and characters apply. true, on, yes, false, off, no, none and null are interpreted, not stored literally, and values containing ?, {, |, &, ~, !, ( or ) must be quoted. Values are strings unless you pass INI_SCANNER_TYPED. And because the input is a string, remember that a line built with single quotes in PHP will not expand \n — use double quotes or a heredoc, or every setting collapses onto one line and the parse fails.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Parse a config blob stored in a database column or fetched from a secrets manager.
  • Merge a heredoc of built-in defaults under user-supplied overrides in tests.
  • Validate ini text a user pasted into an admin form before writing it to disk.

Frequently asked questions

What is the difference between parse_ini_string and parse_ini_file?

parse_ini_string() parses text you already hold in memory, while parse_ini_file() reads from a path on disk. Both accept the same process_sections and scanner_mode arguments and produce identical output for identical content.

Why does parse_ini_string return false?

The text contains a syntax error — most often an unquoted value with a reserved character such as ?, !, & or |, or an ini built with single-quoted PHP strings so the newlines never materialised.

How do I keep integers as integers when parsing ini?

Pass INI_SCANNER_TYPED as the third argument. Without it every value comes back as a string, including numbers, booleans and null.

More examples

Defaults merged with overrides

timeout keeps its default of 30 while retries and verbose take the override values, with correct int and bool types. array_replace() is the right merge here because ini output is a flat string-keyed array.

Example · php
<?php
$defaults = parse_ini_string("timeout = 30\nretries = 3\nverbose = false\n", false, INI_SCANNER_TYPED);
$override = parse_ini_string("retries = 5\nverbose = true\n", false, INI_SCANNER_TYPED);

$config = array_replace($defaults, $override);
print_r($config);

Validating text before saving it

The valid block round-trips to JSON; the second is rejected because ? and ! are reserved unquoted. Parsing before writing keeps a broken config from ever reaching disk.

Example · php
<?php
$submitted = "name = demo\nport = 9000\n";
$bad       = "name = un?quoted!\n";

foreach (['good' => $submitted, 'bad' => $bad] as $label => $text) {
    $parsed = @parse_ini_string($text, false, INI_SCANNER_TYPED);
    echo $label, ': ', $parsed === false ? "REJECTED\n" : json_encode($parsed) . "\n";
}

RAW mode keeps values literal

INI_SCANNER_RAW skips escape processing and constant interpolation, so quotes and backslashes survive exactly as written and "yes" stays the string "yes" rather than becoming "1". Use it when the value is a Windows path, a regex, or anything you want byte-for-byte.

Example · php
<?php
$text = 'path = "C:\\logs\\app"' . "\nflag = yes\n";

echo "NORMAL: "; var_dump(parse_ini_string($text));
echo "RAW:    "; var_dump(parse_ini_string($text, false, INI_SCANNER_RAW));

Discussion

  • Be the first to comment on this lesson.