<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Parse a configuration file</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.move-uploaded-file.html">move_uploaded_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.parse-ini-string.html">parse_ini_string</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.parse-ini-file" class="refentry"> <div class="refnamediv"> <h1 class="refname">parse_ini_file</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">parse_ini_file</span> — <span class="dc-title">Parse a configuration file</span></p> </div> <div class="refsect1 description" id="refsect1-function.parse-ini-file-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>parse_ini_file</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$process_sections</code><span class="initializer"> = false</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$scanner_mode</code><span class="initializer"> = INI_SCANNER_NORMAL</span></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>parse_ini_file()</strong></span> loads in the ini file specified in <em><code class="parameter">filename</code></em>, and returns the settings in it in an associative array. </p> <p class="para"> The structure of the ini file is the same as the <var class="filename">php.ini</var>'s. </p> </div> <div class="refsect1 parameters" id="refsect1-function.parse-ini-file-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">filename</code></em></span> <dd> <p class="para"> The filename of the ini file being parsed. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">process_sections</code></em></span> <dd> <p class="para"> By setting the <em><code class="parameter">process_sections</code></em> parameter to <strong><code>TRUE</code></strong>, you get a multidimensional array, with the section names and settings included. The default for <em><code class="parameter">process_sections</code></em> is <strong><code>FALSE</code></strong> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">scanner_mode</code></em></span> <dd> <p class="para"> Can either be <strong><code>INI_SCANNER_NORMAL</code></strong> (default) or <strong><code>INI_SCANNER_RAW</code></strong>. If <strong><code>INI_SCANNER_RAW</code></strong> is supplied, then option values will not be parsed. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.parse-ini-file-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The settings are returned as an associative <span class="type"><a href="language.types.array.html" class="type array">array</a></span> on success, and <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.parse-ini-file-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> Added optional <em><code class="parameter">scanner_mode</code></em> parameter. Single quotes may now be used around variable assignments. Hash marks (<em>#</em>) may no longer be used as comments and will throw a deprecation warning if used. </td> </tr> <tr> <td>5.2.7</td> <td> On syntax error this function will return <strong><code>FALSE</code></strong> rather than an empty array. </td> </tr> <tr> <td>5.2.4</td> <td> Keys and section names consisting of numbers are now evaluated as PHP <a href="language.types.integer.html" class="link">integers</a> thus numbers starting by 0 are evaluated as octals and numbers starting by 0x are evaluated as hexadecimals. </td> </tr> <tr> <td>5.0.0</td> <td> Values enclosed in double quotes can contain new lines. </td> </tr> <tr> <td>4.2.1</td> <td> This function is now affected by <a href="ini.sect.safe-mode.html#ini.safe-mode" class="link">safe mode</a> and <a href="ini.core.html#ini.open-basedir" class="link">open_basedir</a>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.parse-ini-file-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2414"> <p><strong>Example #1 Contents of <var class="filename">sample.ini</var></strong></p> <div class="example-contents"> <div class="cdata"><pre> ; This is a sample configuration file ; Comments start with ';', as in php.ini [first_section] one = 1 five = 5 animal = BIRD [second_section] path = "/usr/local/bin" URL = "http://www.example.com/~username" [third_section] phpversion[] = "5.0" phpversion[] = "5.1" phpversion[] = "5.2" phpversion[] = "5.3" </pre></div> </div> </div> <div class="example" id="example-2415"> <p><strong>Example #2 <span class="function"><strong>parse_ini_file()</strong></span> example</strong></p> <div class="example-contents"><p> <a href="language.constants.html" class="link">Constants</a> may also be parsed in the ini file so if you define a constant as an ini value before running <span class="function"><strong>parse_ini_file()</strong></span>, it will be integrated into the results. Only ini values are evaluated. For example: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">'BIRD'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Dodo bird'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Parse without sections<br /></span><span style="color: #0000BB">$ini_array </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Parse with sections<br /></span><span style="color: #0000BB">$ini_array </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"sample.ini"</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [one] => 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 ) ) 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 ) ) ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-2416"> <p><strong>Example #3 <span class="function"><strong>parse_ini_file()</strong></span> parsing a php.ini file</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// A simple function used for comparing the results below<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">$expression</span><span style="color: #007700">)<br />{<br /> return(</span><span style="color: #0000BB">$expression </span><span style="color: #007700">? </span><span style="color: #DD0000">'Yes' </span><span style="color: #007700">: </span><span style="color: #DD0000">'No'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// Get the path to php.ini using the php_ini_loaded_file() <br />// function available as of PHP 5.2.4<br /></span><span style="color: #0000BB">$ini_path </span><span style="color: #007700">= </span><span style="color: #0000BB">php_ini_loaded_file</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Parse php.ini<br /></span><span style="color: #0000BB">$ini </span><span style="color: #007700">= </span><span style="color: #0000BB">parse_ini_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini_path</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Print and compare the values, note that using get_cfg_var()<br />// will give the same results for parsed and loaded here<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'(parsed) magic_quotes_gpc = ' </span><span style="color: #007700">. </span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">$ini</span><span style="color: #007700">[</span><span style="color: #DD0000">'magic_quotes_gpc'</span><span style="color: #007700">]) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'(loaded) magic_quotes_gpc = ' </span><span style="color: #007700">. </span><span style="color: #0000BB">yesno</span><span style="color: #007700">(</span><span style="color: #0000BB">get_cfg_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'magic_quotes_gpc'</span><span style="color: #007700">)) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> (parsed) magic_quotes_gpc = Yes (loaded) magic_quotes_gpc = Yes </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.parse-ini-file-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function has nothing to do with the <var class="filename">php.ini</var> 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. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes ("). </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> 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, no and false results in "", yes and true results in "1". Characters <em>?{}|&~![()^"</em> must not be used anywhere in the key and have a special meaning in the value. </span> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.parse-ini-file-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.parse-ini-string.html" class="function" rel="rdfs-seeAlso">parse_ini_string()</a> - Parse a configuration string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.move-uploaded-file.html">move_uploaded_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.parse-ini-string.html">parse_ini_string</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>