Lesson 5 - Working with INI files in PHP New
In the previous lesson, Working with XML files in PHP , we learned how to work with XML files. We discussed the use of the XMLWriter, XMLReader, and SimpleXML classes.
In this lesson, we'll look at the INI file format and learn how to work with
it. The name INI comes from the word
Initialization, which in short
means the assignment of values.
If you have ever needed a database in your web applications, you have
probably used MySQL, which is very popular especially among beginning
developers, but even the advanced ones won't disdain it. It supports a large
part of the SQL standard, which is an important query language allowing to work
with databases, but it's a bit of an overkill for storing data in the
key => value" format.
If you've ever been configuring Windows (especially their very old versions)
or games, you've probably come across files with the
extension. They're plain text files with a simple internal structure in the form
key=value, in which the individual records are separated
by a new line.
Such a database excels mainly in its simplicity. If, for example, we want to save login credentials for a "large" database or make a list of error messages in different human languages and we do not want to insert this data directly into our scripts (because it's good to separate the program from data), the INI file database may certainly come in handy.
INI file types
According to their internal structure, we distinguish these types of INI files:
- flat and
- divided into sections.
Flat INI files
Creating basic flat INI files is very simple and we just need a text editor:
host=localhost user=admin pass=p4ssw0rd name=test
We can save the file under the name
.ht" at the beginning of the file name will make sure,
that nobody will be able to display the contents of this file in a browser. Keep
in mind, that this is only true if we're using the Apache webserver (and have it
configured properly) and that there are also other ways to restrict the direct
access to any file.
Now, we just need to read and process the file in PHP using the
$mysql = parse_ini_file('.htdata.ini'); print_r($mysql);
Yes, that's all - it's that simple. We'll then output the returned
associative array, for example, with the
print_r() function. Our
output of which will be as follows:
Array ( [host] => localhost [user] => admin [pass] => p4ssw0rd [name] => test )
What about speed? PHP loaded a testing data file, which had
100 kB (and contained 2000 keys), in about
10 ms. As
for the number of keys, I would take it as an upper limit. Web applications
don't usually even have that many configuration parameters anyway. For lists of
error messages, this number can easily be higher.
INI section files
The data structure does not have to be just flat, but it's possible to use another level of immersion. The INI file can be divided into sections:
[MySQL] host=localhost user=admin pass=p4ssw0rd name=test [PgSQL] host=localhost user=admin pass=pgp4ssw0rd name=testpg
PHP script to read and extract this file will look very similar. The only
change we need to make is passing
true as the second argument of
parse_ini_file(), which tells PHP that we want to process sections
in the file:
$inidata = parse_ini_file('.htdata.ini', true); print_r($inidata);
The output will look like this:
Array ( [MySQL] => Array ( [host] => localhost [user] => admin [pass] => p4ssw0rd [name] => test ) [PgSQL] => Array ( [host] => localhost [user] => admin [pass] => pgp4ssw0rd [name] => testpg ) )
We created a two-dimensional associative array, which is usually sufficient for more complex configuration files.
There're ways to create and modify INI files from PHP scripts. In general, however, I cannot recommend doing it. It's more common to prepare such a file on a PC and copy it to the server. After all, we said that this file format is primarily suitable for storing configuration parameters, which we usually do not need to change programmatically.
We mentioned multilingual versions of a website. In this case, all you have
to do is create the appropriate number of files (or one file with multiple
sections), each with a list of phrases in one language. The
parse_ini_file() then opens the correct file or section according
to the selected client language. Then it's up to the application to handle the
Properties of INI files
Before concluding the lesson, we'll get to know a few more features of INI files, the knowledge of which could be useful when creating them:-)
Comments are preceded by a semicolon (
;) at the beginning of the
line. Lines with comments will be ignored and we can write anything that will
help us with orientation in the file:
[Section] ; MYSQL password (primary) password=3]T/n*pLu}.$83R! ; MYSQL password (secondary) [email protected]"X7x5(F(}(3z
Some implementations also treat lines beginning with a hashtag
#) as comments, but this is not the case in the current versions
If we pass a
.ini file to PHP containing multiple
records with the same key in one section or multiple sections with the same
name, only the last one will be present in the
resulting array. However, this may be different in other implementations, so we
should never rely on this behaviour:
[MySQL] password=pass1 [MySQL] password=pass2 ; only pass3 will appear in the array password=pass3
After processing such a file, the result will be as follows:
Array ( [MySQL] => Array ( [password] => pass3 ) )
PHP treats keys or sections, whose names differ only in capitalisation as different strings. It's therefore case-sensitive. However, the implementation in e.g. the Windows API does not behave this way, and again this is something you should not rely on:
[MYSQL] password=pass1 [MySQL] PASSWORD=pass2 password=pass3
The output after parsing a file with the contents above will therefore be this:
Array ( [MYSQL] => Array ( [password] => pass1 ) [MySQL] => Array ( [PASSWORD] => pass2 [password] => pass3 ) )
Advantages and disadvantages of using INI files
Using INI files as a database can be beneficial when:
- we need to load configuration data that we do not want to have directly in PHP scripts
- we need to use most of the loaded data, e.g. in a template
- there isn't too much data
- we want to be able to easily edit the data, even from the terminal directly on the server
- the data will only be used in exceptional cases, e.g. error messages
- we need the data to be available even if the main database is unavailable
But everything has its disadvantages. We usually won't want to use this database if:
- there's a lot of data
- we only need to read some of the data
- we need to search for data other than by primary key
- we want to programmatically change data directly from PHP scripts
As always, the source codes for this lesson can be downloaded below.
In the next lesson, Working with ZIP archives in PHP, we'll learn how to work with ZIP archives in PHP.
We'll use the
ZipArchive class and its most important methods.
Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.
DownloadBy downloading the following file, you agree to the license terms
Downloaded 1x (2.29 kB)
Application includes source codes in language PHP
No one has commented yet - be the first!