PHP parse_ini_string() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, parse_ini_string() reads settings written in INI format from a string and returns them as an array.

It works like parse_ini_file(), but you give it the text itself. This is handy when the settings come from a database, an API or a variable in your script.

Syntax

Call parse_ini_string() with the values you need. Start with this line.

PHP
$config = parse_ini_string("name = CodeToFun");
CallWhat it does
parse_ini_string('name = CodeToFun')Returns an array with one key, name.
parse_ini_string($text, true)Keeps the [section] groups in the result.
parse_ini_string($text, false, INI_SCANNER_RAW)Returns every value exactly as written.

The scanner modes work the same as in parse_ini_file(). In normal mode, on, yes and true become 1, and off, no, false and none become an empty string. If the text has a syntax error, PHP shows a warning and the function returns false.

Library declarations
PHP
parse_ini_string(string $ini_string, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL): array|false

Parameters

ParameterDescription
ini_stringRequired. The text in INI format.
process_sectionsOptional. If true, a multidimensional array is returned using the [section] names. The default is false.
scanner_modeOptional. INI_SCANNER_NORMAL (default), INI_SCANNER_RAW or INI_SCANNER_TYPED.

Return Value

Return ValueDescription
array|falseReturns the settings as an associative array, or false on failure.

PHP Examples

Example 1 — Parse a Short INI String

Turn a few lines of text into an array.

PHP
<?php
$ini = '
name = CodeToFun
language = PHP
';

print_r(parse_ini_string($ini));

How It Works

  1. $ini holds two lines in INI format.
  2. parse_ini_string() reads each name = value line.
  3. Blank lines and extra spaces around the equal sign are ignored.
  4. print_r() shows the array that was created.

Example 2 — Broken INI Text

A section that is never closed is a syntax error, so the function returns false.

PHP
<?php
$bad = '[broken section';

$result = @parse_ini_string($bad, true);

if ($result === false) {
    echo "Invalid INI text", PHP_EOL;
}

How It Works

  1. The bracket after the word broken is missing, so the text is not valid INI.
  2. parse_ini_string() raises a warning and returns false.
  3. The @ hides the warning for this demo.
  4. The strict check shows your own error message instead.

Example 3 — Compare Normal and Raw Modes

The same text gives different results depending on the scanner mode.

PHP
<?php
$ini = 'debug = off';

$normal = parse_ini_string($ini);
$raw    = parse_ini_string($ini, false, INI_SCANNER_RAW);

echo 'Normal: [', $normal['debug'], ']', PHP_EOL;
echo 'Raw: [', $raw['debug'], ']', PHP_EOL;

How It Works

  1. In normal mode, the word off is turned into an empty string.
  2. In raw mode, the value is kept exactly as written.
  3. The square brackets make the empty string easy to see in the output.
  4. Choose raw mode when you want the original text.

Frequently Asked Questions

It parses text in INI format and returns an associative array of settings.
Use it when the INI text is already in a variable, for example when it comes from a database or an API.
It returns false and shows a warning. Always check the result with === false.
Pass INI_SCANNER_TYPED as the third argument. Numbers become integers and true or false become booleans.
In normal mode, off, no, false and none become an empty string. Use INI_SCANNER_RAW to keep the original text.

Next: pathinfo()

pathinfo() splits a file path into its parts: the folder, the file name, the extension and.

pathinfo() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful