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

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.

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.";
}