Skip to content
Tutorials Center
My learning

Topics

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
<?php
echo ini_get('precision');   // "14"
ini_set('precision', '5');
echo 1 / 3;                  // 0.33333

Try it

PHP Reading and changing a setting

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.

PHP Error display and memory limits

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

PHP
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 use filter_var($v, FILTER_VALIDATE_BOOLEAN).
  • Shorthand sizes. memory_limit returns values like "128M" or "-1". Convert them yourself as shown above.
  • Not every directive can change at runtime. Each has a mode: PHP_INI_ALL can be set anywhere, others only in php.ini (for example upload_max_filesize is PHP_INI_PERDIR, so ini_set() fails). Check ini_get_all() for the access level.
  • Restore a single setting with ini_restore(), which returns it to the value from php.ini.
  • Security directives such as open_basedir can 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_errors off and log errors instead.
  • Web servers. Directives set in .htaccess, .user.ini or pool configuration are visible through ini_get() but may not be changeable.