Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

parse_ini_file()

Example

What’s within “test.ini”:

[names]
me = Robert
you = Peter

[urls]
first = “http://www.example.com”
second = “https://www.w3schools.com”

PHP script:

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

The following is the result of the code above:

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

Definition and Usage

An configuration (ini) file is parsed by the parse_ini_file() method, which then returns the settings.

Advice: This function is unrelated to the php.ini file and can be used to read in your own configuration files.

Note: The following reserved words—null, yes, no, true, false, on, off, and none—must not be used as keys for ini files. Moreover, the key cannot contain any of the reserved letters {}|&~!()^”.

Syntax

parse_ini_file(fileprocess_sectionsscanner_mode)

Parameter Values

Parameter

Description

file

Essential. indicates which ini file to parse.

process_sections

Not required. It returns a multidimensional array containing section names and settings if set to TRUE. False is the default.

scanner_mode

Not required. may take on any of the subsequent values:
• By default, INI_SCANNER_NORMAL
• INI_SCANNER_RAW, which indicates that option values won’t be processed.

• INI_SCANNER_TYPED (indicating that, when feasible, integer, boolean, and null types are retained). “Yes”, “on”, and “true” are changed to TRUE. “Off”, “no”, “false”, and “none” are changed to FALSE. NULL is created from “null”. If at all possible, numerical strings are transformed to integer type.

Technical Details

Return Value:

False on failure, array on success

PHP Version:

4.0+

PHP Changelog:

PHP 7.0: # symbol (#) is not interpreted as a comment
PHP 5.6.1: INI_SCANNER_TYPED mode was added.
PHP 5.3: The scanner_mode parameter is now optional.

More Examples

Example

What’s within “test.ini”:

[names]
me = Robert
you = Peter

[urls]
first = “http://www.example.com”
second = “https://www.w3schools.com”

PHP script (process_sections = true):

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

The following is the result of the code above:

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