PHP Reference ยท Files and I/O
PHP ini_set() and ini_get() functions
Reference for PHP ini_set(), ini_get() and ini_restore(): change configuration at runtime, see old values returned, which settings can change, and pitfalls.
PHP behavior is controlled by configuration directives such as memory_limit, precision or display_errors. They normally come from php.ini. The function ini_get() reads the current value of a directive, and ini_set() changes it for the rest of the running script.
Changes made with ini_set() last only until the script ends. They do not edit php.ini, and other requests are not affected.
<?php echo ini_get('precision'); // "14" ini_set('precision', '5'); echo 1 / 3; // 0.33333
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
ini_get(string $option): string|false ini_set(string $option, string|int|float|bool|null $value): string|false ini_restore(string $option): void ini_get_all(?string $extension = null, bool $details = true): array|false
| Name | Description |
|---|---|
$option |
Name of the configuration directive, such as memory_limit. |
$value |
New value. Booleans are converted to "1" or "". |
Return value: ini_get() returns the value as a string (an empty string for off or unset), or false if the directive does not exist. ini_set() returns the old value as a string, or false if the directive does not exist or cannot be changed.
Errors / Exceptions: none are thrown. Always compare ini_set()'s result with === false.
Usage notes
- Everything is a string.
ini_get('display_errors')may give"1","","On"or"stderr"depending on how it was written. Normalize before comparing, or usefilter_var($v, FILTER_VALIDATE_BOOLEAN). - Shorthand sizes.
memory_limitreturns values like"128M"or"-1". Convert them yourself as shown above. - Not every directive can change at runtime. Each has a mode:
PHP_INI_ALLcan be set anywhere, others only inphp.ini(for exampleupload_max_filesizeisPHP_INI_PERDIR, soini_set()fails). Checkini_get_all()for the access level. - Restore a single setting with
ini_restore(), which returns it to the value fromphp.ini. - Security directives such as
open_basedircan only become stricter at runtime, never looser. - Prefer dedicated functions when they exist:
error_reporting(),set_time_limit(),date_default_timezone_set(),setlocale(). - Do not hide errors in production code. Turn
display_errorsoff and log errors instead. - Web servers. Directives set in
.htaccess,.user.inior pool configuration are visible throughini_get()but may not be changeable.