How to Retrieve PHP Configuration Values Using ini_get
This article explains how to use the built-in PHP function
ini_get() to retrieve the current value of configuration
options from your environment. You will learn the syntax of the
function, see practical code examples for reading common settings, and
understand how to interpret the returned values, including boolean
configurations.
Understanding the ini_get() Function
The ini_get() function is a built-in PHP tool used to
read the active value of a configuration directive set in your
php.ini file, web server configuration, or
.htaccess file.
Syntax
ini_get(string $option): string|false$option: The name of the configuration option you want to retrieve (e.g.,'upload_max_filesize','display_errors').- Return Value: Returns the value of the
configuration option as a string on success. If the configuration option
does not exist in the PHP environment, it returns
false.
Practical Examples
1. Retrieving String and Integer Values
To get the value of settings like file upload limits or memory limits, pass the setting name as a string argument:
$max_upload = ini_get('upload_max_filesize');
$memory_limit = ini_get('memory_limit');
echo "Maximum upload file size is: " . $max_upload . "\n";
echo "Memory limit is: " . $memory_limit . "\n";2. Handling Boolean Configuration Options
When retrieving boolean settings (such as display_errors
or register_argc_argv), PHP returns them as strings.
- An active boolean option (set to
Onor1) will return the string"1". - An inactive boolean option (set to
Offor0) will return an empty string""or"0".
To safely check a boolean configuration, evaluate it like this:
$display_errors = ini_get('display_errors');
if ($display_errors === '1' || strtolower($display_errors) === 'on') {
echo "Errors are displayed.";
} else {
echo "Errors are hidden.";
}3. Checking for Non-Existent Settings
If you attempt to retrieve a directive that does not exist,
ini_get() returns false. You can use a strict
comparison to handle these cases:
$non_existent = ini_get('invalid_setting_name');
if ($non_existent === false) {
echo "The specified configuration option does not exist.";
}