Home >Backend Development >PHP Tutorial >How to Read and Write to an INI File in PHP?

How to Read and Write to an INI File in PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-29 08:44:02282browse

How to Read and Write to an INI File in PHP?

Reading and Writing to an INI File in PHP

The PHP parse_ini_file function can be used to read values from an INI file into an associative array. To read the value for "lu_link", you would use the following code:

<code class="php">$ini_array = parse_ini_file("sample.ini");
echo $ini_array['lu_link'];</code>

To write a new value for "lu_link", you can use the write_php_ini function shown in the provided answer. Here's an example of how to use it:

<code class="php">$ini_array['lu_link'] = '#000000';
write_php_ini($ini_array, "sample.ini");</code>

Similarly, to read the value for "footerbg", you would use:

<code class="php">echo $ini_array['footerbg'];</code>

And to write a new value for "footerbg":

<code class="php">$ini_array['footerbg'] = '#FFFFFF';
write_php_ini($ini_array, "sample.ini");</code>

The provided write_php_ini function takes an array of key-value pairs and writes them back to the INI file, ensuring that any changes are persistent.

The above is the detailed content of How to Read and Write to an INI File in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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