php用ini_get获取php.ini里变量值的方法 |
|
本文实例讲述了php用ini_get获取php.ini里变量值的方法 。分享给大家供大家参考 。具体分析如下: 要得到php.ini里的变量值,当然,你可以用phpinfo();来得到所有php配置信息,但如果要想得到某个变量值的话,你又要怎样获取呢? php里提供一个获取php.ini里的变量值的函数: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;
}
?>
上述代码的运行结果类似如下: 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的加强函数 ini_get_all() 。 ini_get_all()函数以数组的形式返回整个php的环境变量,用法也很简单 。 实例一:
<?php
print_r(ini_get_all("pcre"));
print_r(ini_get_all());
?>
上述代码的运行结果类似如下:
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
?>
输出结果类似如下: Array ( [pcre.backtrack_limit] => 100000 [pcre.recursion_limit] => 100000 ) Array ( [allow_call_time_pass_reference] => 0 [allow_url_fopen] => 1 ... ) 与ini_get()相对的函数是ini_set(),ini_set具有更改php.ini设置的功能 。例如当某脚本运行超时时,可以设置其最大执行时间 。 希望本文所述对大家的php程序设计有所帮助 。 |