Use ini_set() when you need to attempts to change a PHP configuration option for the current execution in a PHP application. The examples below show the actual operation and its limitations.
What Is ini_set() in PHP?
Attempts to change a PHP configuration option for the current execution. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
ini_set(string $option, string|int|float|bool|null $value): string|false
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
$previous = ini_set("precision","12"); if ($previous !== false) ini_set("precision",$previous); echo "Restored";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
echo ini_get("memory_limit");
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$setting = "display_errors"; $previous = ini_set($setting,"0"); if ($previous !== false) { echo "Temporary setting changed"; ini_set($setting,$previous); }
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
Some settings cannot be changed at runtime; never rely on changing production security configuration from a request.
Comparison With Related Functions
| Approach | Difference |
|---|---|
ini_set() | Attempts to change a PHP configuration option for the current execution. |
| Related operation | ini_get() reads a setting; ini_set() attempts to change it. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Some settings cannot be changed at runtime; never rely on changing production security configuration from a request. |
| Confusing this function with a related operation | ini_get() reads a setting; ini_set() attempts to change it. |
Key Takeaways
- Attempts to change a PHP configuration option for the current execution.
- Some settings cannot be changed at runtime; never rely on changing production security configuration from a request.
- ini_get() reads a setting; ini_set() attempts to change it.
Use ini_set() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.