PHP parse_ini_file()Functions


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual

Definition and Usage

The parse_ini_file() function parses a configuration file (an ini file) and returns the settings it contains in the form of an array.

Syntax

parse_ini_file(file,process_sections)

Parameter Description
file Required. Specifies the ini file to be checked.
process_sections Optional. If set to TRUE, returns a multidimensional array, including the name and settings of each section in the configuration file. Default is FALSE.


Tips and Notes

Tip:This function can be used to read the configuration file of your own application; it has nothing to do with the php.ini file.

Note:Some reserved words cannot be used as key names in ini files, including: null, yes, no, true, and false. The characters {}|&~![()" also cannot be used anywhere in key names.


Example 1

Contents of "test.ini":

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "https://www.example.com"

PHP code:

<?php
print_r(parse_ini_file("test.ini"));
?>

The code above will output:

Array
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => https://www.example.com
)


Example 2

Contents of "test.ini":

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3cschool.cc"

PHP code (with process_sections set to true):

<?php
print_r(parse_ini_file("test.ini",true));
?>

The code above will output:

Array
(
[names] => Array
(
[me] => Robert
[you] => Peter
)
[urls] => Array
(
[first] => http://www.example.com
[second] => http://www.w3cschool.cc
)
)


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual Other Extensions