Home  >  Article  >  Backend Development  >  PHP最常用的ini函数分析 针对PHP.ini配置文件_PHP

PHP最常用的ini函数分析 针对PHP.ini配置文件_PHP

WBOY
WBOYOriginal
2016-06-01 12:19:591065browse

* ini_get():获取配置文件的选项值

这个函数相信很多人都使过,就是获取配置文件中某一个选项的值,如果是true值就返回1,如果是false值就返回0,字符串就返回字符串。

比如手册中的例子:

<?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";
?>

输出:

display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9

这个函数主要是为了获取配置文件,可以方便你很多操作。比如你想操作字符串过滤,但是又不清楚magic_quotes_gpc有没有打开,所以你就可以这样写一个函数:

/* 字符串过滤函数 */
function stringFilter($str)
{
if (ini_get('magic_quotes_gpc)') {
return $str;
} else {
return addslashes($str);
}
}

当然,如果你无法知道你的全局变量是否打开,也可以定制这样的函数:

/* 变量检测函数 */
function getGetVar($var)
{
if (ini_set('register_gobals')) {
return $var;
} else {
return $_GET['var'];
}
}

当然,你可以做很多用途,自己慢慢体会。

* ini_set函数:设置php.ini中的某些变量值

这个函数是设置选项中的值,在执行函数后生效,脚本结束的时候,这个设置也失效。不是所有的选项都能被改函数设置的。具体那些值能够设置,可以查看手册中的列表。

就是能够设置php.ini中的选项值比如,display_error选项关闭了,但是你要显示程序中的错误信息,方便你调试程序,那么就可以使用这个函数:

ini_set("display_errors", "On");

那么在你这个页面的程序都会显示错误信息了,而且你还可以使用error_reporting来设置显示的错误信息级别。

如果你需要增加脚本执行时间,那么可以设置:

ini_set("max_execution_time", "180");

那么脚本执行时间就由默认的30秒变为180秒,当然,你也可以使用set_time_limit()来设置。

其实你把ini_set和ini_get结合使的话,非常好。比如你想在配置文件里添加自己的包含文件路径,但是你有没有权限更改php.ini,那么你可以结合两个函数:

ini_set('include_path',ini_get('include_path').':/your_include_dir:');

* ini_get_all: 获取所有的设置选项变量

把所有选项值以数组的形式返回,方便你当phpinfo()无法使用的时候来使用。

手册例子,比如:

<?php
$inis = ini_get_all();

print_r($inis);

?>

部分输出:

Array
(
[allow_call_time_pass_reference] => Array
(
[global_value] => 1
[local_value] => 1
[access] => 6
)
[allow_url_fopen] => Array
(
[global_value] => 1
[local_value] => 1
[access] => 7
)
...
)

* ini_restore: 回复配置文件默认的值

就是回复配置文件默认的值,当你使用ini_set设置后可以使用它来恢复。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn