Home > Article > Backend Development > PHP parse_ini_file() function
Definition and usage
parse_ini_file() function parses a configuration file and returns the settings in the form of an array.
Syntax
Parameters | Description |
---|---|
file | Required. Specifies the ini file to check. |
process_sections | Optional. If set to true, returns a multidimensional array containing the name and settings of each section in the configuration file. The default is false. |
Explanation
The structure of the ini file is similar to that of php.ini.
Constants can also be parsed in ini files, so if a constant is defined as an ini value before running parse_ini_file(), it will be integrated into the result. Only the value of ini will be evaluated.
Key names and section names composed of numbers will be treated as integers by PHP, so numbers starting with 0 will be treated as octal and numbers starting with 0x will be treated as hexadecimal.
Tips and Notes
Notes: This function can be used to read the configuration file of your own application. This function has nothing to do with the php.ini file, which is already processed when running the script.
Note: If the value in the ini file contains any non-alphanumeric characters, it needs to be enclosed in double quotes (").
Note: Some reserved words cannot be used as key names in the ini file, Includes: null, yes, no, true and false. Values of null, no and false are equivalent to "", and values of yes and true are equivalent to "1". The characters {}|"~![()" cannot be used either. Used anywhere in key names, and these characters have special meaning in option values.
Note: Since PHP 5.0, this function also handles new lines within option values
Example
Example 1
Contents of "test.ini":
PHP code:
Output:
Example 2
Contents of "test.ini":
PHP code (process_sections set to true ):
Output:
The above introduces the PHP parse_ini_file() function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.