How a config.php file can be your best friend

hammer and screwdriver icon for config settingsI use a config file in almost every PHP applications I write. Essentially I create an array called $config and setting everything I would like to be able to quickly change. For example: app name, full path to app, full url to app, development or production mode, even mysql settings. Then I can either simply pass $config to every function or make it a global variable where needed. Here is an example of what a config file might look like:

 

$config['debug']="yes";

if($config['debug']=="yes") {
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", 1);
}

$config['locals']=array(
'127.0.0.1',
'::1',
'localhost'
);

//-------------------------------------------------------------------------------
//paths
$config['site']='SiteName';
$config['siteurl']='//' . $_SERVER['HTTP_HOST'];
$config['pageurl']='//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

//-------------------------------------------------------------------------------
//contact page

$config['contact-to']="scott@somedomain.com;
$config['contact-subject']="SiteName Contact";

//-------------------------------------------------------------------------------
//mailgun settings

$config['mg-key']="******************";
$config['mg-domain']='mg.somedomain.com'; // Use your domain
$config['mg-uniquekey']="******************"; //Any string will work, used for confirming signip
$config['mg-mailinglist']='newsletter@mg.somedomain.com'; // Create a MailingList in the control Panel first

//-------------------------------------------------------------------------------
//mysql

$config['is']['mysql_host']="localhost";
$config['is']['mysql_username']="username";
$config['is']['mysql_password']="somecrazypassword";
$config['is']['mysql_db']="dbName";
$config['is']['mysql_table1']="table1";
$config['is']['mysql_table2]="table2";
$config['is']['mysql_table3']="table3";
?>

 


Tags: MySQL, PHP