PHP parse_ini_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, parse_ini_file() reads an INI configuration file and returns its settings as an array.

INI files are a simple way to store settings such as the site name, database host or debug mode, with one name = value pair on each line.

Syntax

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

PHP
$config = parse_ini_file('config.ini');
CallWhat it does
parse_ini_file('config.ini')Returns a flat array of all name and value pairs.
parse_ini_file('config.ini', true)Groups settings by their [section] names.
parse_ini_file('config.ini', true, INI_SCANNER_TYPED)Also converts true, false and numbers to real PHP types.

With the default INI_SCANNER_NORMAL mode, the words true, on and yes become the string 1, and false, off, no and none become an empty string. Use INI_SCANNER_TYPED to get real booleans and integers, or INI_SCANNER_RAW to keep every value exactly as written.

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

Parameters

ParameterDescription
filenameRequired. The path to the INI file.
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 — Read a Simple Config File

Write a small INI file and read it back as an array.

PHP
<?php
$file = __DIR__ . '/config.ini';

$text = <<<'INI'
app_name = CodeToFun
debug = true
port = 8080
INI;

file_put_contents($file, $text);

print_r(parse_ini_file($file));

How It Works

  1. The INI text is saved to config. ini with file_put_contents().
  2. parse_ini_file() reads each name = value line into the array.
  3. debug = true becomes the string 1 in the default mode.
  4. The port stays a string, because normal mode does not convert numbers.

Example 2 — A Missing Config File

parse_ini_file() returns false when the file cannot be read.

PHP
<?php
$config = @parse_ini_file(__DIR__ . '/missing.ini');

if ($config === false) {
    echo "Config file could not be read", PHP_EOL;
}

How It Works

  1. missing. ini does not exist.
  2. parse_ini_file() cannot open it and returns false.
  3. The @ hides the warning so you can print your own message.
  4. A strict === false check avoids mixing it up with an empty array.

Example 3 — Sections and Typed Values

Use sections and INI_SCANNER_TYPED to get real integers and booleans.

PHP
<?php
$file = __DIR__ . '/app.ini';

$text = <<<'INI'
[database]
host = localhost
port = 3306
persistent = true
INI;

file_put_contents($file, $text);

$config = parse_ini_file($file, true, INI_SCANNER_TYPED);

var_dump($config['database']['host']);
var_dump($config['database']['port']);
var_dump($config['database']['persistent']);

How It Works

  1. The [database] line starts a section named database.
  2. true as the second argument keeps the sections as nested arrays.
  3. INI_SCANNER_TYPED converts 3306 to an integer and true to a boolean.
  4. var_dump() shows the exact type of each value.

Frequently Asked Questions

It reads an INI file and returns its settings as an associative array.
Pass true as the second argument. The result will be an array of arrays, one for each section.
In the default mode, true, on and yes are turned into the string 1. Use INI_SCANNER_TYPED to get a real boolean.
parse_ini_file() reads from a file on disk. parse_ini_string() reads from a string you already have in memory.
Only if the file is outside your public web folder. Never place INI files with passwords where visitors can download them.

Next: parse_ini_string()

parse_ini_string() reads settings written in INI format from a string and returns them as .

parse_ini_string() 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