PHP 5.4.31 Released

ini_get

(PHP 4, PHP 5)

ini_getGets the value of a configuration option

Description

string ini_get ( string $varname )

Returns the value of the configuration option on success.

Parameters

varname

The configuration option name.

Return Values

Returns the value of the configuration option as a string on success, or an empty string for null values. Returns FALSE if the configuration option doesn't exist.

Examples

Example #1 A few ini_get() examples

<?php
/*
Our php.ini contains the following settings:

display_errors = On
register_globals = Off
post_max_size = 8M
*/

echo 'display_errors = ' ini_get('display_errors') . "\n";
echo 
'register_globals = ' ini_get('register_globals') . "\n";
echo 
'post_max_size = ' ini_get('post_max_size') . "\n";
echo 
'post_max_size+1 = ' . (ini_get('post_max_size')+1) . "\n";
echo 
'post_max_size in bytes = ' return_bytes(ini_get('post_max_size'));

function 
return_bytes($val) {
    
$val trim($val);
    
$last strtolower($val[strlen($val)-1]);
    switch(
$last) {
        
// The 'G' modifier is available since PHP 5.1.0
        
case 'g':
            
$val *= 1024;
        case 
'm':
            
$val *= 1024;
        case 
'k':
            
$val *= 1024;
    }

    return 
$val;
}

?>

The above example will output something similar to:


display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9
post_max_size in bytes = 8388608

Notes

Note: When querying boolean values

A boolean ini value of off will be returned as an empty string or "0" while a boolean ini value of on will be returned as "1". The function can also return the literal string of INI value.

Note: When querying memory size values

Many ini memory size values, such as upload_max_filesize, are stored in the php.ini file in shorthand notation. ini_get() will return the exact string stored in the php.ini file and NOT its integer equivalent. Attempting normal arithmetic functions on these values will not have otherwise expected results. The example above shows one way to convert shorthand notation into bytes, much like how the PHP source does it.

Changelog

Version Description
5.3.0 Previously, the empty string was returned if the configuration option didn't exist. now, FALSE is returned instead.

See Also

add a note add a note

User Contributed Notes 8 notes

up
9
Stas Trefilov, OpteamIS
4 years ago
another version of return_bytes which returns faster and does not use multiple multiplications (sorry:). even if it is resolved at compile time it is not a good practice;
no local variables are allocated;
the trim() is omitted (php already trimmed values when reading php.ini file);
strtolower() is replaced by second case which wins us one more function call for the price of doubling the number of cases to process (may slower the worst-case scenario when ariving to default: takes six comparisons instead of three comparisons and a function call);
cases are ordered by most frequent goes first (uppercase M-values being the default sizes);
specs say we must handle integer sizes so float values are converted to integers and 0.8G becomes 0;
'Gb', 'Mb', 'Kb' shorthand byte options are not implemented since are not in specs, see
http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes

<?php
function return_bytes ($size_str)
{
    switch (
substr ($size_str, -1))
    {
        case
'M': case 'm': return (int)$size_str * 1048576;
        case
'K': case 'k': return (int)$size_str * 1024;
        case
'G': case 'g': return (int)$size_str * 1073741824;
        default: return
$size_str;
    }
}
?>
up
2
Ivo Mandalski
2 years ago
This version of return_bytes takes care of the MB, GB, KB cases along with the M,G,K ones.
Hope this is helpful!

<?php
   
public static function return_bytes ($val)
    {
        if(empty(
$val))return 0;

       
$val = trim($val);

       
preg_match('#([0-9]+)[\s]*([a-z]+)#i', $val, $matches);

       
$last = '';
        if(isset(
$matches[2])){
           
$last = $matches[2];
        }

        if(isset(
$matches[1])){
           
$val = (int) $matches[1];
        }

        switch (
strtolower($last))
        {
            case
'g':
            case
'gb':
               
$val *= 1024;
            case
'm':
            case
'mb':
               
$val *= 1024;
            case
'k':
            case
'kb':
               
$val *= 1024;
        }

        return (int)
$val;
    }
?>
up
1
david dot tulloh at infaze dot com dot au
9 years ago
You can set custom entries in the ini file to provide globals such as database details.
However these must be retrieved with get_cfg_var, ini_get won't work.
up
1
filh at filh dot org
8 years ago
Concerning the value retourned, it depends on how you set it.
I had the problem with horde-3 which test the safe_mode value.
THan :
- if you set the value with php_admin_value safe_mode Off (or On) ini_get returns the string
- if you set the value with php_admin_flag safe_mode Off (or On) ini_get returns the boolean.
up
1
Der Graph
9 years ago
It might be useful for included scripts that include other files to extend the 'include_path' variable:

<?php ini_set('include_path',ini_get('include_path').':../includes:');  ?>

Sometimes, it may also be useful to store the current 'include_path' in a variable, overwrite it, include, and then restore the old 'include_path'.
up
2
peter
6 years ago
The above example function called return_bytes() assumes that ini_get('upload_max_filesize') delivers only one letter at the end. As I've seen 'Mb' and things like that, I'd suggest to change the $last = ... part into $last = strtolower(substr($val,strlen($val/1),1)).
I'd call it $unit then.
up
0
espertalhao04 at hotmail dot com
1 hour ago
I was needing a small function to return the memory limit in a system, in whatever value was in php.ini

I made this tiny function (can be reduced) which works well (at least on 64bit systems, may not work on 32bit systems for GB values).

<?php
   
function memory_limit()
    {
        if(!
function_exists('ini_get')||($m=@ini_get('memory_limit'))===null)return null;
       
$x=array(''=>1,'k'=>1024,'m'=>1048576,'g'=>1073741824);
        return(
$n=$m/1)*$x[strtolower(str_replace($n,'',$m))];
    }
?>

Readable and commented version:
<?php
   
function memory_limit()
    {
       
//verifies if the function ini_get exists and is enabled
        //(it's impossible to avoid using the error suppression operator here)
       
if( !function_exists('ini_get') || ($memory = @ini_get('memory_limit')) === null )
            return
null;
           
       
//this will be used to multiply by the final value, to obtain the size in bytes
       
$units=array(
           
//this is used when there is no unit
            //when there is no unit, php interprets as if the value
            // was already a value in bytes
           
''=>1,
           
'k'=>1024,//kbytes
           
'm'=>1048576,//mbytes = 1024 * 1024
           
'g'=>1073741824 //gbytes = 1024 * 1024 * 1024
       
);
       
       
$amount = $memory/1;//convert the value to a number
       
        //removes the memory amount, leaving behind only the unit if present
       
$unit = strtolower(str_replace($amount,'',$memory));
       
       
//makes the calculation
       
return $amount*$units[$unit];
    }

?>

This might not be the fastest, but surely its the simplest way.
up
-2
nicolas dot grekas+php at gmail dot com
4 years ago
Here is how to accurately test for boolean php.ini values:

<?php

function ini_get_bool($a)
{
   
$b = ini_get($a);

    switch (
strtolower($b))
    {
    case
'on':
    case
'yes':
    case
'true':
        return
'assert.active' !== $a;

    case
'stdout':
    case
'stderr':
        return
'display_errors' === $a;

    default:
        return (bool) (int)
$b;
    }
}

?>
To Top