Home PHP Filesystem parse_ini_file() PHP parse_ini_file() Function 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.
$config = parse_ini_file('config.ini'); Call What 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 parse_ini_file(string $filename, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL): array|false Parameters Parameter Description filename Required. The path to the INI file. process_sections Optional. If true, a multidimensional array is returned using the [section] names. The default is false. scanner_mode Optional. INI_SCANNER_NORMAL (default), INI_SCANNER_RAW or INI_SCANNER_TYPED.
Return Value Return Value Description array|false Returns the settings as an associative array, or false on failure.
PHP Examples 1. Read a Simple Config File 2. A Missing Config File 3. Sections and Typed Values Example 1 — Read a Simple Config File Write a small INI file and read it back as an array.
<?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)); Array
(
[app_name] => CodeToFun
[debug] => 1
[port] => 8080
) How It Works The INI text is saved to config. ini with file_put_contents().parse_ini_file() reads each name = value line into the array. debug = true becomes the string 1 in the default mode. 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
$config = @parse_ini_file(__DIR__ . '/missing.ini');
if ($config === false) {
echo "Config file could not be read", PHP_EOL;
} Config file could not be read How It Works missing. ini does not exist.parse_ini_file() cannot open it and returns false. The @ hides the warning so you can print your own message. A strict === false check avoids mixing it up with an empty array. Frequently Asked Questions What does parse_ini_file() do in PHP? It reads an INI file and returns its settings as an associative array.
How do I keep the [sections] in the result? Pass true as the second argument. The result will be an array of arrays, one for each section.
Why does true become 1 in parse_ini_file()? In the default mode, true, on and yes are turned into the string 1. Use INI_SCANNER_TYPED to get a real boolean.
What is the difference between parse_ini_file() and parse_ini_string()? parse_ini_file() reads from a file on disk. parse_ini_string() reads from a string you already have in memory.
Is parse_ini_file() safe for secrets? Only if the file is outside your public web folder. Never place INI files with passwords where visitors can download them.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion