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;
}
}
?>
ini_get
(PHP 4, PHP 5)
ini_get — Bir yapılandırma yönergesinin değerini döndürür
Açıklama
Belirtilen yapılandırma yönergesinin değerini döndürür.
Değiştirgeler
- yönerge
-
yapılandırma yönergesinin ismi.
Dönen Değerler
Başarısızlık durumunda ve NULL değerler için boş bir dizge, aksi takdirde yapılandırma yönergesinin değeri ile döner.
Örnekler
Örnek 1 - ini_get() örnekleri
<?php
/*
php.ini dosyamız şu ayarları içeriyor:
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 (bayt olarak) = ' . return_bytes(ini_get('post_max_size'));
function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// 'G' birimi PHP 5.1.0 sürümünden beri var.
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
?>
Yukarıdaki örnek şuna benzer bir çıktı üretir:
display_errors = 1 register_globals = 0 post_max_size = 8M post_max_size+1 = 9 post_max_size (bayt olarak) = 8388608
Notlar
Bilginize: Mantıksal değerleri sorgularken
Bir mantıksal ini değeri olarak off, boş bir dizge veya "0" olarak dönerken, on değeri "1" olarak dönecektir. Bu işlev ayrıca yönergenin değerini birebir dizge olarak da döndürebilir.
Bilginize: Bellek miktarı değerlerini sorgularken
upload_max_filesize yönergesinde olduğu gibi pek çok bellek miktarı değeri, php.ini dosyasında kısa gösterimleriyle saklanabilir. ini_get() işlevi, yönergenin değeri olarak değerin tamsayı karşılığını değil, php.ini dosyasında saklandığı haliyle dizge değerini döndürür. Bu bakımdan, bu değer üzerindeki aritmetik işlemler beklenen sonuçları vermeyebilir. Yukarıdaki örnekte, kısa gösterimi bayt cinsinden bir değere dönüştürmenin yolu gösterilmektedir.
Ayrıca Bakınız
- get_cfg_var() - Belirtilen PHP yönergesinin değerini döndürür
- ini_get_all() - Tüm yapılandırma yönergelerini döndürür
- ini_restore() - Bir yapılandırma yönergesine eski değerini atar
- ini_set() - Bir yapılandırma yönergesine değer atar
ini_get
19-Aug-2009 05:51
15-Mar-2009 10:20
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;
}
?>
29-Jul-2008 09:25
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.
21-Nov-2005 02:24
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.
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.
22-Jun-2005 02:01
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.
16-Aug-2004 06:59
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'.
13-Aug-2002 02:29
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().
