PHP: ini_get - Manual (original) (raw)
(PHP 4, PHP 5, PHP 7, PHP 8)
ini_get — Gets the value of a configuration option
Description
Parameters
option
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](reserved.constants.php#constant.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
opcache.enable_cli = Off
post_max_size = 8M
*/
echo 'display_errors = ' . ini_get('display_errors') . "\n";
echo 'opcache.enable_cli = ' . (int) ini_get('opcache.enable_cli') . "\n";
echo 'post_max_size = ' . ini_get('post_max_size') . "\n";
echo 'post_max_size + 1 = ' . (rtrim(ini_get('post_max_size'), 'KMG') + 1) . "\n";
echo 'post_max_size in bytes = ' . ini_parse_quantity(ini_get('post_max_size'));?>`
The above example will output something similar to:
display_errors = 1 opcache.enable_cli = 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
offwill be returned as an empty string or "0" while a boolean ini value ofonwill 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 asupload_max_filesize, are stored in the php.ini file in shorthand notation.ini_get() will return the exact string stored in thephp.ini file and NOT its int equivalent. Attempting normal arithmetic functions on these values will not have otherwise expected results. Theini_parse_quantity() function can be used to convert the shorthand notation into bytes.
Note:
ini_get() can't read "array" ini options such as
pdo.dsn.*, and returns[false](reserved.constants.php#constant.false)in this case.
See Also
- get_cfg_var() - Gets the value of a PHP configuration option
- ini_get_all() - Gets all configuration options
- ini_parse_quantity() - Get interpreted size from ini shorthand syntax
- ini_restore() - Restores the value of a configuration option
- ini_set() - Sets the value of a configuration option
Found A Problem?
15 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;
}
}
?>
4 years ago
Here is another way to get the result in bytes using PHP8
<?php
/**
* @param string $size
* @return int
* @author DevsrealmGuy
*/
public function getBytes(string $size): int
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>i</mi><mi>m</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">size = trim(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6595em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">i</span><span class="mord mathnormal">ze</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">im</span><span class="mopen">(</span></span></span></span>size);
#
# Separate the value from the metric(i.e MB, GB, KB)
#
preg_match('/([0-9]+)[\s]*([a-zA-Z]+)/', <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">size, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">i</span><span class="mord mathnormal">ze</span><span class="mpunct">,</span></span></span></span>matches);
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>s</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">value = (isset(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">i</span><span class="mord mathnormal">sse</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>matches[1])) ? $matches[1] : 0;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>e</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>c</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>s</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">metric = (isset(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6595em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">i</span><span class="mord mathnormal">sse</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>matches[2])) ? strtolower($matches[2]) : 'b';
#
# Result of $value multiplied by the matched case
# Note: (1024 ** 2) is same as (1024 * 1024) or pow(1024, 2)
#
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mo>∗</mo><mo>=</mo><mi>m</mi><mi>a</mi><mi>t</mi><mi>c</mi><mi>h</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">value *= match (</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">e</span><span class="mord">∗</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">ma</span><span class="mord mathnormal">t</span><span class="mord mathnormal">c</span><span class="mord mathnormal">h</span><span class="mopen">(</span></span></span></span>metric) {
'k', 'kb' => 1024,
'm', 'mb' => (1024 ** 2),
'g', 'gb' => (1024 ** 3),
't', 'tb' => (1024 ** 4),
default => 0
};
return (int)$value;
}
#
# TEST: This default to 0 if it doesn't conform with the match standard
#
echo getBytes('2GB') . "</br>";
# OUTPUT: 2147483648
echo getBytes('4tb') . "</br>";
# OUTPUT: 4398046511104
echo getBytes('5345etrgrfd') . "</br>";
# OUTPUT: 0
echo getBytes('357568336586') . "</br>";
# OUTPUT: 0
?>
8 years ago
Be aware that max_execution_time can be altered by XDebug.
While debugging a script locally that made use of <?php ini_get('max_execution_time'); ?> it returned 0 when XDebug remote debugging was enabled and the IDE was listening to it.
It makes sense, since debugging manually takes time so we don't want the script to time out ; but in that particular case, it made it look to the script like max_execution_time was 0, so calculations were wrong.
You can see in phpinfo() that local value is 0 in that case, but master value is the correct one you set in your php.ini.
20 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.
14 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;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>i</mi><mi>m</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">val = trim(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">im</span><span class="mopen">(</span></span></span></span>val);
preg_match('#([0-9]+)[\s]*([a-z]+)#i', <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">val, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span></span></span></span>matches);
$last = '';
if(isset($matches[2])){
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>s</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">last = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>matches[2];
}
if(isset($matches[1])){
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">val = (int) </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mclose">)</span></span></span></span>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;
}
?>Joe Huss detain at interserver dot net ¶
3 years ago
Here is a version combining a few of the examples here that does *not* require php8 nor does it generate a warning
/**
* gets the value in bytes converted from a human readable string like 10G'
*
* @param mixed $val the human readable/shorthand version of the value
* @return int the value converted to bytes
*/
function return_bytes($val) {
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>t</mi><mi>r</mi><mi>i</mi><mi>m</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">val = trim(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">im</span><span class="mopen">(</span></span></span></span>val);
preg_match('/([0-9]+)[\s]*([a-zA-Z]+)/', <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">val, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span></span></span></span>matches);
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>s</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">value = (isset(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">i</span><span class="mord mathnormal">sse</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>matches[1])) ? intval($matches[1]) : 0;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>e</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>c</mi><mo>=</mo><mo stretchy="false">(</mo><mi>i</mi><mi>s</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">metric = (isset(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6595em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span><span class="mord mathnormal">c</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">i</span><span class="mord mathnormal">sse</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>matches[2])) ? strtolower($matches[2]) : 'b';
switch ($metric) {
case 'tb':
case 't':
$value *= 1024;
case 'gb':
case 'g':
$value *= 1024;
case 'mb':
case 'm':
$value *= 1024;
case 'kb':
case 'k':
$value *= 1024;
}
return $value;
}david dot tulloh at infaze dot com dot au ¶
20 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.
10 years ago
Yet another implementation of return_bytes:
<?php
function return_bytes($val)
{
assert('1 === preg_match("/^\d+([kmg])?$/i", $val)');
static $map = array ('k' => 1024, 'm' => 1048576, 'g' => 1073741824);
return (int)$val * @($map[strtolower(substr($val, -1))] ?: 1);
}
?>
If you're using PHP >= 7, you might replace ?: with ?? to avoid the use of the @ silencer.
17 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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>s</mi><mi>t</mi><mo>=</mo><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>t</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi>o</mi></mrow><annotation encoding="application/x-tex">last = ... part into </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord">...</span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">t</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mord mathnormal">o</span></span></span></span>last = strtolower(substr($val,strlen($val/1),1)).
I'd call it $unit then.nicolas dot grekas+php at gmail dot com ¶
16 years ago
Here is how to accurately test for boolean php.ini values:
<?php
function ini_get_bool($a)
{
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>i</mi><mi>n</mi><msub><mi>i</mi><mi>g</mi></msub><mi>e</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">b = ini_get(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">in</span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">g</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>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;
}
}
?>
21 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'.
10 years ago
In a similar vein, converting flags to booleans proper:
<?php
function return_boolean($val)
{
static $map = array ('on' => true, 'true' => true, 'off' => false, 'false' => false);
return @($map[strtolower($val)] ?: (bool)$val);
}
?>
If you're using PHP >= 7, consider replacing ?: with ?? and removing the @ silencer.