Loading INI Files in PHP

Published: April 14th, 2009 by:

PHP parses the php.ini file for the global PHP settings, but you can create an INI file that is specific to your own PHP application. You can also load those settings into as associate array with a native PHP function. How cool is that?


Why?

Having your config settings in a INI file makes it easier to edit, especially for those that don’t have PHP experience. It also comes in handy if you have protected PHP scripts, and you want someone to have the option of changing settings without having access to any of the PHP code. There are a couple negative effects though. Obviously, there is going to be some performance overhead because PHP will have to parse this file for each and every request made by a site visitor. Also, you can’t assign any dynamic values here, like time(), etc. You will have to make the call to determine if this method is best for you.

How it Works

Below is a sample INI file that the later examples will be loading. Notice the syntax used. The double quotes are not needed, but I am in the habit of using them because they are required if any special characters are in the values.

;some general settings not related to anything else
[general]
home_url = "http://www.example.com/"
contact_email = "contact@example.com"
app_path = "/path/to/app/root"
name = "application name"

;settings required to connect to a MySQL database
[database]
host = "localhost"
name = "database_name"
user = "db_user"
pass = "db_password"

;show we can have an array
[arrays]
test[] = "value1"
test[] = "value2"
test[] = "value3"

In the first load example, we are going to load all of the settings in one block. In other words, we are going to ignore the sections, defined in the ‘[ ]’ characters.

Run This Example

<?php

/* load the config file and dump the values  */
$config = parse_ini_file('sample.ini');

header('Content-type: text/plain');
var_dump($config);

/**
 * Why do I comment out the PHP closing tag?
 * See: http://phpstarter.net/2009/01/omit-the-php-closing-tag/
 */
/* ?> */

In the above example, we have a problem. Notice that we have the setting “name” in more than one block, and because of that, we had a name conflict and lost one of the values. To avoid this, we can put these setting blocks in their own associative array with the following statement.

Run This Example

<?php

/* load the config settings and dump the values */
$config = parse_ini_file('sample.ini', true);

header('Content-type: text/plain');
var_dump($config);

/**
 * Why do I comment out the PHP closing tag?
 * See: http://phpstarter.net/2009/01/omit-the-php-closing-tag/
 */
/* ?> */

Now all the setting blocks are separated, and easily accessible.

So there you have it – a different type of configuration file that may be an option for your next PHP application. It’s pretty simple, and you really only need one function call to load all the settings, so the learning curve is minimal. If you want more information on this PHP function, check out the function reference.


One Response to “Loading INI Files in PHP”

  • Jordan

    I haven’t seen many PHP applications that actually use .ini files. The standard generally is config.inc files with actual PHP Variables. I don’t know why either, ini files are easy to read and easy to format.

    I used your article to answer a PHP question on one of my sites. Kudos!

     

Leave a Reply





Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Andrew has been coding PHP applications since 2006, and has plenty of experience with PHP, MySQL, and Apache. He prefers Ubuntu Linux on his desktop and has plenty of experience at managing CentOS web servers. He is the owner of Wells IT Solutions LLC, and develops PHP applications full time for anyone that needs it as well as does desktop computer support locally in the local area. He spends most of his free time exploring new programming concepts and posting on The Webmaster Forums.