parse_ini_file

PHP 4, PHP 5, PHP 7, PHP 8
parse_ini_file - Parse a configuration file
Manual
Code Examples

parse_ini_file(
     string$filename,
     [bool$process_sections = false],
     [int$scanner_mode = INI_SCANNER_NORMAL]
): array|false

parse_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.

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.

As of PHP 5.6.1 can also be specified as INI_SCANNER_TYPED. In this mode boolean, null and integer types are preserved when possible. String values "true", "on" and "yes" are converted to true. "false", "off", "no" and "none" are considered false. "null" is converted to null in typed mode. Also, all numeric strings are converted to integer type if it is possible.

Return Values

The settings are returned as an associative array on success, and false on failure.

Notes

Note:

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.

Note:

If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").

Note:

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.

Note:

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.

Note:

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.

Related Functions

Example of parse_ini_file

Show all examples for parse_ini_file

PHP Version: