Home > Backend Development > PHP Tutorial > Analysis of PHP's most commonly used ini function for PHP.ini configuration file_PHP tutorial

Analysis of PHP's most commonly used ini function for PHP.ini configuration file_PHP tutorial

WBOY
Release: 2016-07-21 15:38:53
Original
882 people have browsed it

* 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");

Then the script execution time changes from the default 30 seconds to 180 seconds. Of course, you can also use set_time_limit() to set it.

In fact, it is very good if you combine ini_set and ini_get. For example, if you want to add your own include file path to the configuration file, but do you have permission to change php.ini, then you can combine the two functions:

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

* ini_get_all: Get all setting option variables

Return all option values ​​​​in the form of an array, which is convenient for you to use when phpinfo() cannot be used.

Manual examples, such as:

<?php
$inis = ini_get_all();

print_r($inis);

?>

Partial output:

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: Restore the default value of the configuration file

is to restore the default value of the configuration file. You can use it to restore after you use ini_set settings.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321715.htmlTechArticle* ini_get(): Get the option value of the configuration file. I believe many people have used this function, which is to get the configuration file. The value of one of the options. If it is a true value, it will return 1. If it is a false value, it will...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template