Home > Backend Development > PHP Tutorial > PHP parse_ini_file() function

PHP parse_ini_file() function

WBOY
Release: 2016-08-08 09:31:54
Original
1591 people have browsed it

Definition and usage

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

Syntax

parse_ini_file(file,process_sections)
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":

[names] me = Robert you = Peter [urls] first = "http://www.example.com" second = "http://www.w3school.com.cn"

PHP code:

Output:

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

Example 2

Contents of "test.ini":

[names] me = Robert you = Peter [urls] first = "http://www.example.com" second = "http://www.w3school.com.cn"

PHP code (process_sections set to true ):

Output:

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

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template