ISSUE :
 
Profile photo
Submitted by Michael Gardnar
on June 6th, 2011 at 07:50:21 EST

Config file in PHP?

Is there way to have configuration file in PHP? I need to store my environment level variable in a config file like my database connection, backend service url etc. When I use to do ASP.NET, there use to be web.config file. Is there any concept of global configuration file in PHP? Please help.


Profile photo
Replied by SBajra
on June 6th, 2011 at 22:12:16 EST
There is no built in concept of configuration file like web.config in PHP. However, you can create your own separate php file for just storing configuration values and include this configuration file to all other php pages as configuration file.

Sample configuration file for php (config.php):
<?php
define("_MyConfigVar1", "Hello");
define("_MyConfigVar2", "world");
?>


Including this configuration file (config.php) in other php file:
<?php
require_once("config.php");
echo _MyConfigVar1;
?>


I hope this helps.
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Michael Gardnar
on June 6th, 2011 at 22:47:06 EST
thanks. This is exactly I am looking for.