Current Location: Home> Latest Articles> What Is the Difference Between ini_restore() and ini_set()? When Should You Use Each One?

What Is the Difference Between ini_restore() and ini_set()? When Should You Use Each One?

gitbox 2025-08-25

1. ini_set() Function

The ini_set() function is used to set the value of a PHP configuration option. It allows developers to modify certain PHP settings at runtime, thereby influencing the script’s behavior. ini_set() can change most PHP directives, but some settings can only take effect if changed in the php.ini configuration file or at the web server level.

Syntax:

<span><span><span class="hljs-title function_ invoke__">ini_set</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$varname</span></span><span>, </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$newvalue</span></span><span>): </span><span><span class="hljs-keyword">string</span></span>|</span><span><span class="hljs-literal">false</span></span><span>  
</span></span>
  • $varname: The name of the configuration option to set.

  • $newvalue: The new value for the option.

Example:

<span><span><span class="hljs-comment">// Set PHP error reporting level</span></span><span>  
</span><span><span class="hljs-title function_ invoke__">ini_set</span></span><span>(</span><span><span class="hljs-string">&#039;error_reporting&#039;</span></span><span>, E_ALL);  
</span></span>

ini_set() is effective during the execution of the script, and its scope is usually limited to the script’s lifecycle. Once the script ends, the changes no longer apply. It may also be restricted by php.ini; for example, certain settings (like max_execution_time) cannot be changed using this function.

2. ini_restore() Function

The ini_restore() function is used to restore a PHP configuration option that was previously changed with ini_set() back to its default value. In other words, it undoes the modifications made by ini_set(), resetting the option to the value defined in the PHP configuration file or server-level settings.

Syntax:

<span><span><span class="hljs-title function_ invoke__">ini_restore</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$varname</span></span><span>): </span><span><span class="hljs-keyword">bool</span></span><span>  
</span></span>
  • $varname: The name of the configuration option to restore.

Example:

<span><span><span class="hljs-comment">// Restore configuration option</span></span><span>  
</span><span><span class="hljs-title function_ invoke__">ini_restore</span></span><span>(</span><span><span class="hljs-string">&#039;error_reporting&#039;</span></span><span>);  
</span></span>

When ini_restore() is called, it only restores configuration options that were changed by ini_set(), and the reset takes effect immediately.

3. Differences and Use Cases

  1. Functional Difference:

    • ini_set() is used to set configuration values at runtime.

    • ini_restore() is used to revert configuration options modified by ini_set() back to their default values.

  2. Scope:

    • ini_set() can modify most PHP configuration options and is suitable for scenarios where temporary script adjustments are needed.

    • ini_restore() only affects options modified via ini_set() and is typically used when default configurations need to be restored.

  3. Persistence:

    • ini_set() changes only persist during the execution of the current script.

    • ini_restore() undoes those changes within the same script.

  4. Limitations:

    • Both ini_set() and ini_restore() may be restricted by the php.ini configuration file; some settings cannot be changed at runtime.

4. When to Use ini_set() and ini_restore()?

  • Use ini_set():

    • When you need to change a configuration option during script execution, such as adjusting error reporting levels or setting file upload size limits.

    • It is ideal for temporary adjustments without permanently changing the server’s configuration.

  • Use ini_restore():

    • When you want to undo configuration changes made with ini_set() and restore the default value.

    • For example, if a configuration option was changed inside a function or code block, you can restore it afterward to avoid affecting other parts of the application.

5. Summary

Both ini_set() and ini_restore() are essential tools in PHP for managing runtime configuration. ini_set() is used to modify configuration options, typically for specific script needs, while ini_restore() reverts them back to their previous state. Using them appropriately allows developers to control the PHP environment more flexibly and ensure that scripts run as expected.