How to Create Config Files in PHP
- Store the Configurations in an Array in a PHP File
- Typecast the Array Configuration to an Object in a PHP File
- Use an INI File to Create a Config File in PHP
This article will introduce some methods to create config files in PHP.
Store the Configurations in an Array in a PHP File
We can create an array of the configuration items to form a config file in PHP.
An associative array will be best for storing the configuration data. The file can be included in another PHP file, and the configuration can be used.
We can use the include()
function to have the configuration file. We will demonstrate the creation of config files for the database connection in PHP.
For example, create a PHP file config.php
and create an array. Write the keys hostname
, username
, and password
in the array.
Set the hostname
to localhost
, username
, and password
to your database username and password. Then return the array.
Next, create another file, DB.php
, and use the include()
function to include the config.php
file and store it in the $conf
variable. Create the variables $hostname
, $username
, $password
, and $db
to assign the configurations.
Store the configurations in the variables accessing each array items of the $conf
variable. Set your database name in the $db
variable. Use the mysqli_connect()
function to connect the server and the database.
Provide the variables we created above as the parameters to the mysqli_connect()
function. If the correct configuration is provided in the config.php
file, the example below will connect the server and the database.
In this way, we can use an array to create a configuration file in PHP.
return array(
'hostname' => 'localhost',
'username' => 'root',
'password' => 'pass123'
);
$conf = include('config.php');
$hostname = $conf['hostname'];
$username = $conf['username'];
$password = $conf['password'];
$db = "my_db";
$con = mysqli_connect($hostname, $username, $password,$db);
if (!$con) {
die("Failed to establish connection");
}
echo "Connection established successfully";
Output:
Connection established successfully
Typecast the Array Configuration to an Object in a PHP File
This method will typecast the array in the config.php
file into an object. In this way, the configurations can be accessed as an object in the PHP file.
Furthermore, we can take the benefits from objects for data handling purposes. For example, the objects could easily be passed JSON to the client-side if we use JavaScript.
For example, typecast the array placing (object)
before the array
function after the return
keyword. Then, in index.php
, access the configurations as $conf->hostname
as shown in the example below.
return (object) array (
'hostname' => 'localhost',
'username' => 'root',
'password' => 'subodh',
'db' => 'oop'
);
$conf = include('config.php');
$hostname = $conf->hostname;
$username = $conf->username;
$password = $conf->password;
$db = $conf->db;
$con = mysqli_connect($hostname, $username, $password,$db);
if (!$con) {
die("Failed to establish connection");
}
echo "Connection established successfully";
Use an INI File to Create a Config File in PHP
We can also create a config file using the INI file in PHP. We can store all the configurations in the INI file and use the parse_ini_file()
function to access those configurations in the PHP file.
The INI file is broadly used as a configuration file. The structure of the file contains key-value pairs.
The parse_ini_file
loads the content of the INI file as an associative array. We will demonstrate the creation of the INI config file to establish a connection to the server and the database.
For example, create a file config.ini
and write the keys hostname
, username
, password
, and db
. Then, write the correct configurations. An example is shown below.
hostname = 'localhost'
username = 'root'
password = 'pass1234'
db = 'my_db'
Next, create an index.php
file and create an $ini
variable in it.
Use the parse_ini_file()
function to parse the config.ini
file. The $ini
variable contains an associative array of the configurations.
Now use the mysqli_connect()
function with the correct parameters to establish the connection as we did above.
$ini = parse_ini_file('config.ini');
$hostname = $ini['hostname'];
$username = $ini['username'];
$password = $ini['password'];
$db = $ini['db'];
$con = mysqli_connect($hostname, $username, $password,$db);
if (!$con) {
die("Failed to establish connection");
}
echo "Connection established successfully";
Output:
Connection established successfully
It should be noted that the INI file should be kept in a non-public folder so that we do not encounter any security problems. In this way, we can create a configuration file with an INI file.
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn