parse_ini_fileParse a configuration file
&reftitle.description;
arrayfalseparse_ini_filestringfilenameboolprocess_sections&false;intscanner_modeINI_SCANNER_NORMALparse_ini_file loads in the
ini file specified in filename,
and returns the settings in it in an associative array.
The structure of the ini file is the same as the &php.ini;'s.
&reftitle.parameters;
filename
The filename of the ini file being parsed. If a relative path is used,
it is evaluated relative to the current working directory, then the
include_path.
process_sections
By setting the process_sections
parameter to &true;, you get a multidimensional array, with
the section names and settings included. The default
for process_sections is &false;
scanner_mode
Can either be INI_SCANNER_NORMAL (default) or
INI_SCANNER_RAW. If INI_SCANNER_RAW
is supplied, then option values will not be parsed.
&ini.scanner.typed;
&reftitle.returnvalues;
The settings are returned as an associative array on success,
and &false; on failure.
&reftitle.examples;
Contents of sample.iniparse_ini_file example
Constants (but not "magic constants" like __FILE__)
may also be parsed
in the ini file so if you define a constant as an ini value before
running parse_ini_file, it will be integrated into
the results. Only ini values are evaluated, and the value must be just the constant. For example:
]]>
&example.outputs.similar;
1
[five] => 5
[animal] => Dodo bird
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
[phpversion] => Array
(
[0] => 5.0
[1] => 5.1
[2] => 5.2
[3] => 5.3
)
[urls] => Array
(
[svn] => http://svn.php.net
[git] => http://git.php.net
)
)
Array
(
[first_section] => Array
(
[one] => 1
[five] => 5
[animal] => Dodo bird
)
[second_section] => Array
(
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
[third_section] => Array
(
[phpversion] => Array
(
[0] => 5.0
[1] => 5.1
[2] => 5.2
[3] => 5.3
)
[urls] => Array
(
[svn] => http://svn.php.net
[git] => http://git.php.net
)
)
)
]]>
parse_ini_file parsing a php.ini file
]]>
&example.outputs.similar;
Value Interpolation
In addition to evaluating constants, certain characters have special meaning in an ini value.
Additionally, environment variables and previously defined configuration options (see get_cfg_var) may be read using
${} syntax.
Escaping Characters
Some characters have special meaning in double-quoted strings and must be escaped by the backslash prefix.
First of all, these are the double quote " as the boundary marker, and the backslash \ itself
(if followed by one of the special characters):
There is an exception made for Windows-like paths: it's possible to not escape trailing backslash
if the quoted string is directly followed by a linebreak:
If one does need to escape double quote followed by linebreak in a multiline value,
it's possible to use value concatenation in the following way
(there is one double-quoted string directly followed by another one):
Another character with special meaning is $ (the dollar sign).
It must be escaped if followed by the open curly brace:
Escaping characters is not supported in the INI_SCANNER_RAW mode
(in this mode all characters are processed "as is").
Note that the ini parser doesn't support standard escape sequences (\n, \t, etc.).
If necessary, post-process the result of parse_ini_file with stripcslashes function.
&reftitle.notes;
This function has nothing to do with the
&php.ini; file. It is already processed by
the time you run your script. This function can be used to
read in your own application's configuration files.
If a value in the ini file contains any non-alphanumeric
characters it needs to be enclosed in double-quotes (").
There are reserved words which must not be used as keys for
ini files. These include: null, yes,
no, true, false,
on, off, none.
Values null, off, no and
false result in "", and values
on, yes and true result
in "1", unless INI_SCANNER_TYPED mode is used.
Characters ?{}|&~!()^" must not be used anywhere in
the key and have a special meaning in the value.
Entries without an equal sign are ignored. For example, "foo"
is ignored whereas "bar =" is parsed and added with an empty
value. For example, MySQL has a "no-auto-rehash" setting
in my.cnf that does not take a value, so
it is ignored.
ini files are generally treated as plain text by web servers and thus served to browsers if requested.
That means for security you must either keep your ini files outside of your docroot or reconfigure your
web server to not serve them. Failure to do either of those may introduce a security risk.
&reftitle.seealso;
parse_ini_string