Parsing INI files in PHP

There are two built-in PHP functions to parse .ini files: parse_ini_file and parse_ini_string.

Both work the same way, with parse_ini_file it’s just required to read the file, e.g. with file_get_contents.


Let’s talk about the parameters. They are the same for both functions. I’ll take a look at parse_ini_file, since it’s the function you will probably use most.

parse_ini_file( string $filename, bool $process_sections, int $scanner_mode );

  • $filename should be self-explanatory
  • $process_sections decides whether to parse the header sections (e.g. [database]) as well. Setting this to true will create a multidimensional array.
  • Possible values: INI_SCANNER_NORMAL (default), INI_SCANNER_RAW, INI_SCANNER_TYPED. INI_SCANNER_TYPED tries to preserve data types (I normally use this); int stays int, “on”, “true”, “yes” is boolean true etc.

The return value is either an array on success or false.


public static funcion getConfig()
$inifile = ‘config/config.ini’;
$config = parse_ini_file($inifile, false, INI_SCANNER_TYPED);
if ($config !== false) {
#return (object)$config;
return $config;
trigger_error(‘Configuration file not found or erroneous.’);
return false;

2 thoughts on “Parsing INI files in PHP”

  1. Hey! I realize this is kind of off-topic however I
    had to ask. Does operating a well-established blog like yours take
    a lot of work? I’m completely new to blogging however I do write in my diary on a daily basis.
    I’d like to start a blog so I can easily share my own experience and feelings online.

    Please let me know if you have any recommendations or tips for
    brand new aspiring bloggers. Thankyou!

    1. Hey, if you have off-topic questions, you can send me an email, please see Imprint.
      To answer your question: Yes and no. The initial setup does take up some time, but once the setup is done, adding content is easy and quick.

Leave a Reply to Robin Cancel reply

Your email address will not be published. Required fields are marked *

4 + thirteen =