Php – the_config.ini vs the_config.php

configuration-filesiniphp

At work we use a .ini file to set variables prior to calling the rest of the framework (I think it goes

function getConfigVars(){
    //read my_config.ini file
    ....
    //call framework
}

and I have always wondered if there was a benefit to doing it that way.

It seems to me that you then have to write access rules to stop people from looking at it from the web and php has to parse it and understand it.

So, why use my_config.ini rather than my_config.php? It is not like anyone should be touching it after it is set up and it seems more convenient to just call the variables and be able to have your IDE auto complete the text wherever you are using the ini variables / parse it for errors.

Best Solution

For those who come to this question because they want to know if there are any performance differences between using an INI file which must be parsed and a PHP file which is simply included (And can be cached by PHP): Yes, there are differences but they are so small that it doesn't really matter.

My benchmark scenario is a config.ini file with 20 key/value pairs and a config.php file with the same 20 key/value pairs written as defines. PHP version is 5.4.9 on Ubuntu Linux 13.04.

key1 = value1
...
key20 = value20

vs.

<?php
define("key1", "value1");
...
define("key2", "value20");

Two test scripts including the configs:

<?php
$CONF = parse_ini_file("config.ini");

vs.

<?php
require_once "config.php";

I tested the performance with ab -c 25 -n 10000.

Result without a PHP cache:

ini: Requests per second:    2660.89 [#/sec] (mean)
php: Requests per second:    2642.28 [#/sec] (mean)

Result with APC PHP cache:

ini: Requests per second:    3294.47 [#/sec] (mean)
php: Requests per second:    3307.89 [#/sec] (mean)

I ran the tests multiple times, naturally the numbers will vary every time but the consensus is: config.ini is a little bit faster when no PHP cache is used, config.php is a little bit faster when a PHP cache is used. But the difference is so small that the decision should not be based on performance.

Related Question