PHP Function Reference

PHP parse_ini_file() Function



The PHP parse_ini_file() function parses a configuration (ini) file returns the settings in it as an associative array. The structure of the ini file is the same as the php.ini's.

Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, off, no and false result in "", and values on, yes and true result in "1", unless INI_SCANNER_TYPED mode is used. Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value.

Note: This function has nothing to do with the php.ini file. It is already processed by the time you run your script. This function can be used to read in your own application's configuration files.

Note: If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").

Syntax

parse_ini_file(filename, process_sections, scanner_mode)

Parameters

ini_string Required. Specify the contents of the ini file being parsed.
process_sections Optional. If set to true, it returns a multidimensional array, with the section names and settings included. Default is false
scanner_mode Optional. It can be one of the following values:
  • INI_SCANNER_NORMAL (default)
  • INI_SCANNER_RAW (means option values will not be parsed)
  • INI_SCANNER_TYPED (means that boolean, null and integer types are preserved when possible. "true", "on", "yes" are converted to true. "false", "off", "no" and "none" are converted to false. "null" is converted to null in typed mode. Numeric strings are converted to integer type if possible)

Return Value

Returns settings as an associative array on success, and false on failure.

Example: parse_ini_file() example

Lets assume that we have a file called test.ini. This file contains following content:

; This is a sample configuration file
; Comments start with ";", as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"

urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"

The example below shows how to use the parse_ini_file() function to parse this file.

<?php
//parsing ini file and 
//displaying the result
print_r(parse_ini_file("test.ini"));
?>

The output of the above code will be:

Array
(
    [one] => 1
    [five] => 5
    [animal] => BIRD
    [path] => /usr/local/bin
    [URL] => http://www.example.com/~username
    [phpversion] => Array
        (
            [0] => 5.0
            [1] => 5.1
            [2] => 5.2
            [3] => 5.3
        )

    [urls] => Array
        (
            [svn] => http://svn.php.net
            [git] => http://git.php.net
        )

)

Example: process sections

In the example below, the same file is parsed with sections.

<?php
//parsing with sections
print_r(parse_ini_file("test.ini", true));
?>

The output of the above code will be:

Array
(
    [first_section] => Array
        (
            [one] => 1
            [five] => 5
            [animal] => BIRD
        )

    [second_section] => Array
        (
            [path] => /usr/local/bin
            [URL] => http://www.example.com/~username
        )

    [third_section] => Array
        (
            [phpversion] => Array
                (
                    [0] => 5.0
                    [1] => 5.1
                    [2] => 5.2
                    [3] => 5.3
                )

            [urls] => Array
                (
                    [svn] => http://svn.php.net
                    [git] => http://git.php.net
                )

        )

)

❮ PHP Filesystem Reference