|
|
|
|
|
要得到php.ini里的變量值,當(dāng)然,你可以用phpinfo();來得到所有php配置信息,但如果要想得到某個變量值的話,你又要怎樣獲取呢?
php里提供一個獲取php.ini里的變量值的函數(shù):ini_get()
ini_get()的用法非常簡單,下面通過實例說明它是如何使用的。
語法:
string ini_get ( string varname )
返回值如果為布爾型則為0或1
實例:
<?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;
}
?>
上述代碼的運行結(jié)果類似如下:
display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9
post_max_size in bytes = 8388608
如果想獲取整個php.ini里的變量值,我們可以用ini_get的加強(qiáng)函數(shù) ini_get_all()。
ini_get_all()函數(shù)以數(shù)組的形式返回整個php的環(huán)境變量,用法也很簡單。
實例一:
<?php
print_r(ini_get_all("pcre"));
print_r(ini_get_all());
?>
上述代碼的運行結(jié)果類似如下:
Array
(
[pcre.backtrack_limit] => Array
(
[global_value] => 100000
[local_value] => 100000
[access] => 7
)
[pcre.recursion_limit] => Array
(
[global_value] => 100000
[local_value] => 100000
[access] => 7
)
)
Array
(
[allow_call_time_pass_reference] => Array
(
[global_value] => 0
[local_value] => 0
[access] => 6
)
[allow_url_fopen] => Array
(
[global_value] => 1
[local_value] => 1
[access] => 4
)
...
)
實例二:
<?php
print_r(ini_get_all("pcre", false)); // Added in PHP 5.3.0
print_r(ini_get_all(null, false)); // Added in PHP 5.3.0
?>
輸出結(jié)果類似如下:
Array
(
[pcre.backtrack_limit] => 100000
[pcre.recursion_limit] => 100000
)
Array
(
[allow_call_time_pass_reference] => 0
[allow_url_fopen] => 1
...
)
與ini_get()相對的函數(shù)是ini_set(),ini_set具有更改php.ini設(shè)置的功能。例如當(dāng)某腳本運行超時時,可以設(shè)置其最大執(zhí)行時間。