Reading configuration files
In your PHP applications, it is recommended to separate the configuration information (database host, username, password, etc.) from your code to a separate file so the configuration can be swapped without changing the code itself.
A very well-known file format is the INI file format which drives the configuration of complex software products like PHP. For example, take a look at php.ini
. Here are some chunks from the default php.ini
file that shows very well the structure of INI files:
[mail function] ; For Win32 only. ; https://php.net/smtp SMTP=localhost ; https://php.net/smtp-port smtp_port=25 [Date] date.timezone=Europe/Berlin [MySQL] mysql.allow_local_infile=On mysql.allow_persistent=On mysql.cache_size=2000
So, there are sections that start with a section headline in square brackets, comments that start with a semicolon, and settings in the format name=value
.
The PHP function parse_ini_file()
reads a configuration file and creates an array out of it.
parse_ini_file()
<?php //Syntax parse_ini_file(string $filename, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL ): array|false
The parse_ini_file() function greatly simplifies the creation of our settings parser, it takes three parameters:
$filename
The filename of the ini configuration file being parsed.$process_sections
The default value isfalse
which returns all settings at once in a simple array. By setting this parameter to true, you can parse the ini file by sections which returns a multidimensional array with the section names and settings included as subarrays. See process_sections example.$scanner_mode
The default value isINI_SCANNER_NORMAL
. The valueINI_SCANNER_RAW
indicates that the function should not parse option values.
Example: Reading/parsing an ini configuration file:
We created a config file config.ini
for our PHP application:
[Emails] admin=admin@brainbell.com team=info@brainbell.com [DB] host=localhost name=dbname user=dbuser pass=passwordx port=3306 [Cache] html.file=On
The parse_ini_file()
function converts the contents of the config file into an associative array:
<?php $ini = parse_ini_file('config.ini'); echo '<pre>'; print_r($ini);
The above code prints the following result on the web browser:
Array ( [admin] => admin@brainbell.com [team] => info@brainbell.com [host] => localhost [name] => dbname [user] => dbuser [pass] => passwordx [port] => 3306 [html.file] => 1 )
Example: Reading configuration of a specific section
If you want to group INI file sections, just set the second parameter to true
in parse_ini_file();
then, you get each section in a subarray, and within those subarrays, you find the names and the values of directives in the INI file:
<?php $ini = parse_ini_file('config.ini', true); echo '<pre>'; print_r($ini);
The contents of config.ini
as a multidimensional array:
Array ( [Emails] => Array ( [admin] => admin@brainbell.com [team] => info@brainbell.com ) [DB] => Array ( [host] => localhost [name] => dbname [user] => dbuser [pass] => passwordx [port] => 3306 ) [Cache] => Array ( [html.file] => 1 ) )
Reading/parsing specific section:
<?php $ini = parse_ini_file('config.ini', true); $emails = $ini['Emails']; echo $emails['admin'] . '<br>'; echo $emails['team']; /* Prints: admin@brainbell.com info@brainbell.com */
Parsing INI Strings
<?php //Syntax parse_ini_string( string $ini_string, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL ): array|false
The parse_ini_string()
function works similarly to the parse_ini_file()
function with one difference, it parses a string. This function takes three parameters:
$ini_string
The contents of the ini file being parsed.$process_sections
If it is set to true, a multidimensional array with values for the sections in the file is returned.$scanner_mode
This parameter is eitherINI_SCANNER_NORMAL
, the default, orINI_SCANNER_RAW
, indicating that the function should not parse option values.
Example: using parse_ini_string()
<?php $str = '[Emails] admin=admin@brainbell.com team=info@brainbell.com [DB] host=localhost name=dbname user=dbuser pass=passwordx port=3306 [Cache] html.file=On'; $ini = parse_ini_string($str, true); $emails = $ini['Emails']; echo $emails['admin'] . '<br>'; echo $emails['team']; /* Prints: admin@brainbell.com info@brainbell.com */
Working with Files in PHP:
- Returning or Downloading Files with an HTTP Request
- Reading a File into a String or Array
- PHP Opening and Closing Files
- Reading files by line or by character
- Writing and appending to files
- Reading and Writing CSV Files
- Parsing INI Files and Strings
- Check File Type (whether a file is a directory or a file)
- Understanding file Permissions in PHP
- Reading Information About Files in PHP
- Copying, Moving and Deleting Files in PHP
- Reading Directories Contents in PHP
- Browse directories recursively
- Zipping and Unzipping a File with Zlib and Bzip2
- Zip and Unzip Archives with ZipArchive
- Using Relative Paths for File Access