questions about config.php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

questions about config.php

Post by PHPycho »

Hello forums
We all know its effective to create one central config file say "config.php"
that contains the required configuration variables .
I had seen the configuration is done by three ways
1> By simply using variables ex $dbHost = ""; etc
2> By using associative arrays ex $setting['dbHost'] = ""; etc
3> By defining constants ex define("DB_HOST",""); etc

My Question ?
Which one to use and why in config.php for creating the configuration file?
Thanks in advance to all of you
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Your forgetting an ini file which can be read by parse_ini_file. I know there has previously been fairly heated debate about this topic... Should constants be used etc previously with different people having lots of different ideas as to using constants etc.

Generally I like the ini file approach as you can easily define parameters in different groupings and process individual groupings as required. An ini file is also more friendly to those people who have never touched php and need to change something. Saying that I sometimes use a phpvars file containing common variables used throughout the system which I simply include. This file however generally contains parameters which are never going to change even for a new installation.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Using an array like $settings would generally be the most useful and safe. Constants are global data so they are more likely to be accessible from anywhere (not always a good idea). It's generally recommended to unset database credentials from any config variables after you've finished using them also.
Post Reply