Login |  Register 
I think this organisation will be one of the big players of the future in the PHP world.
Darren
 

PHP Configuration files

When ever you build a decent size application you will probably need to save configuration values. While you can hardcode configuration values this can make your code more brittle as you will have to update these configuration changes everywhere they are hardcoded when you want to update the value. Which could be quite a pain if this is stored in 10+ files!

The easiest way to get around this is by using PHP's built in configuration capacity.

To do this create your own configuration file, in this case its called config.ini.

DatabaseName = "phprocks"
Hostname = "localhost"
Username = "test"
Password = "password123"
This can then be read with the parse_ini_file function, like so:
$config = parse_ini_file("config.ini");
//will output phprocks
echo $config['DatabaseName'];
If you have a lot of configuration values this can be split down into sections so the config file would be modified to be:
[Database]
DatabaseName = "phprocks"
Hostname = "localhost"
Username = "test"
Password = "password123"
[Site]
SiteTitle = "PHP Rocks! - "
So to use the configuration file with sections you need to specify an extra parameter in the parse_ini_file function as shown below.
$config = parse_ini_file("config.ini", true);
//will output phprocks
echo $config['Database']['DatabaseName'];
This returns a multidimensional array which is why you must first specify the section you want so in the example above Database before the configuration element DatabaseName

Comments to date: 7. Page 1 of 1. Average Rating:
umesh   4:56am on Saturday, November 25th, 2017 
good one use full
prashant   4:59am on Friday, September 8th, 2017 
very useful
zenzuiski   6:17pm on Wednesday, April 20th, 2016 
very nice very good
djcool9   3:04am on Tuesday, July 28th, 2015 
After all I found something amazing.
roja   12:58am on Saturday, June 13th, 2015 
useful
ngorn Khy   5:44pm on Wednesday, November 26th, 2014 
very good lesson to help learner in php programming
thanks
Waleed   8:46pm on Thursday, April 14th, 2011 
Simply Amazing. After going through so many tutorial, this configuration thing is useful and new to me..
PHP-Rocks..You Rocks.

security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38