Parsing INI Files

Another file format that is very well-known is the INI file formatit was very widely used in the Windows world, but today also drives the configuration of complex software products like PHP.

(parse_ini_file('php.ini', true)

For instance, take a look at php.ini. Here is a (modified) excerpt from the default php.ini that shows very well the structure of INI files:

[mail function]
; For Win32 only.
; SMTP = localhost
; For Unix only.  You may supply arguments as well
   (default: "sendmail -t -i").
sendmail_path =

So, there are sections that start with a section headline in square brackets, comments that start with a semicolon, and settings in the format name=value. The PHP function parse_ini_file() now reads in such a PHP file and creates an array out of it: Each section is a subarray, and within those subarrays you find the names and the values of directives in the INI file. The code at the beginning of This does this (remember to change the path to php.ini, if appropriate), and figure shows the output.

Reading Information from INI Files

  echo '<xmp>' .
    print_r(parse_ini_file('php.ini', true), true) .

The contents of php.ini as a multidimensional array (parse_ini_file.php; excerpt).

If you want to avoid the grouping by sections in the INI file, just omit the second parameter parse_ini_file(); then, you get all settings at once.

by BrainBellupdated