downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

ini_restore> <ini_get_all
[edit] Last updated: Fri, 17 May 2013

view this page in

ini_get

(PHP 4, PHP 5)

ini_getRestituisce il valore delle opzioni di configurazione

Descrizione

string ini_get ( string $varname )

Restituisce il valore delle opzioni di configurazione. In caso di errore, tipo la richiesta per un valore inesistente, sarà restituita una stringa vuota.

Nota: Richieste per valori booleani

Nel file ini, il valore booleano off sarà restituito come stringa vuota o "0", mentre il valore on sarà restituito come "1".

Nota: Richieste per le dimensioni della memoria

Diversi parametri attinenti alle dimensioni di memoria, tipo upload_max_filesize sono registrati nel php.ini in notazione abbreviata. La funzione ini_get() restituirà l'esatto valore presente nel php.ini, e NON l'intero equivalente. L'esecuzione delle normali funzioni aritmetiche su questi valori potrà dare risultati inattesi. L'esempio seguente illustra come si possa convertire la notazione breve in byte in modo molto simile a come fa il PHP.

Example #1 Qualche esempio di ini_get()

<?php
/*
Il nostro php.ini contiene i seguenti parametri:

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 $val{strlen($val)-1};
    switch(
$last) {
        case 
'k':
        case 
'K':
            return (int) 
$val 1024;
            break;
        case 
'm':
        case 
'M':
            return (int) 
$val 1048576;
            break;
        default:
            return 
$val;
    }
}
?>

Lo script produrrà:


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

Vedere anche: get_cfg_var(), ini_get_all(), ini_restore() e ini_set().



ini_restore> <ini_get_all
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes ini_get - [11 notes]
up
4
Ivo Mandalski
1 year 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
2
Stas Trefilov, OpteamIS
3 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
0
filh at filh dot org
7 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
nicolas dot grekas+php at gmail dot com
3 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;
    }
}

?>
up
-1
Anonymous
7 years ago
Important: The manual says that ini_get will return 0 or an empty string for boolean config values that are set to off in php.ini.

This is technically correct, however when you use

php_value register_globals off

in an .htaccess file, ini_get will return the string, which will "evaluate" to 1. So if you are using mod_php you have to check boolean config values against the strings (upper/lowercase etc.) anyhow or you will get wrong results.
up
0
peter
4 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
david dot tulloh at infaze dot com dot au
7 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
Nathaniel Sabanski
4 years ago
Here's a simplified version of return_bytes() that does not rely on non-breaking cases and variable variables.

<?php

function return_bytes($val)
{
   
$val = trim($val);
   
$last = strtolower(substr($val, -1));
   
    if(
$last == 'g')
       
$val = $val*1024*1024*1024;
    if(
$last == 'm')
       
$val = $val*1024*1024;
    if(
$last == 'k')
       
$val = $val*1024;
       
    return
$val;
}

?>
up
-1
Der Graph
8 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
-1
fbeyer at <nospam>clickhand dot de
10 years ago
If you want to test ini flags (eg. On/Off), I recommend to explicitly cast the value returned by ini_get() to boolean - it is cleaner as you only get true or false, not 0 or 1 or "" as described above.

<?php
    $register_globals
= (bool) ini_get('register_gobals');
?>

C fans may of course also cast it to (int) to play with 0 and 1 - that's also cleaner to print().
up
-2
Dmitry Vinogradov
9 months ago
Here's more comprehensive function returning number of bytes from strings like max_post_size or upload_max_filesize. It handles both 'k'/'kb', 'm'/'mb', and 'g'/'gb' at the end of the string, and is case insensitive.

<?php
function return_bytes($val)
{
   
$val = trim($val);
   
    switch (
strtolower(substr($val, -1)))
    {
        case
'm': $val = (int)substr($val, 0, -1) * 1048576; break;
        case
'k': $val = (int)substr($val, 0, -1) * 1024; break;
        case
'g': $val = (int)substr($val, 0, -1) * 1073741824; break;
        case
'b':
            switch (
strtolower(substr($val, -2, 1)))
            {
                case
'm': $val = (int)substr($val, 0, -2) * 1048576; break;
                case
'k': $val = (int)substr($val, 0, -2) * 1024; break;
                case
'g': $val = (int)substr($val, 0, -2) * 1073741824; break;
                default : break;
            } break;
        default: break;
    }
    return
$val;
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites