Home  >  Article  >  Backend Development  >  Usage examples of ini_set and ini_get in PHP, ini_setini_get_PHP tutorial

Usage examples of ini_set and ini_get in PHP, ini_setini_get_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:10803browse

Usage examples of ini_set and ini_get in PHP, ini_setini_get

The examples in this article describe the usage of ini_set and ini_get in PHP. Share it with everyone for your reference. The details are as follows:

PHP ini_set function.

The PHP ini_set function is to set the value in the option. It takes effect after the function is executed. When the script ends, this setting also becomes invalid. Not all options can be set by this function. The specific values ​​that can be set can be viewed in the manual for a list.

You can set the option value in php.ini. For example, if the display_error option is turned off, but you want to display the error message in the program to facilitate your debugging of the program, then you can use the PHP ini_set function:

Copy code The code is as follows:
ini_set("display_errors", "On");

Then the program on your page will display error information, and you can also use error_reporting to set the level of error information displayed.

If you need to increase the script execution time, you can set:

Copy code The code is as follows:
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 the PHP ini_set function with 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 two functions:

Copy code The code is as follows:
ini_set('include_path',ini_get('include_path').':/your_include_dir:');


The functions in PHP have different functions. Only by using these functions flexibly can the language play a great role.

I believe many people have used the PHP configuration function ini_get(), which is to get the value of an option in the configuration file. If it is a true value, it returns 1, if it is a false value, it returns 0, and if it is a false value, it returns a string.

For example, the example in the manual:

Copy code The code is as follows:
< ?php
echo 'display_errors = ' . ini_get('display_errors') . " "; //Whether error display is turned on
echo 'register_globals = ' . ini_get('register_globals') . " ";//Whether global variables are open
echo 'post_max_size = ' . ini_get('post_max_size') . " ";//The maximum file size that can be submitted
echo 'post_max_size+1 = ' . (ini_get('post_max_size')+1) . " ";
?>

Output:
​display_errors = 1
​register_globals = 0
Post_max_size = 8M
Post_max_size+1 = 9

The PHP configuration function ini_get() is mainly used to obtain the configuration file, which can facilitate many operations for you. For example, you want to operate string filtering, but you don’t know whether magic_quotes_gpc is turned on, so you can write a function like this:

Copy code The code is as follows:
function stringFilter($str)
{
If (ini_get('magic_quotes_gpc)')
{
        return $str;
}else{
         return addslashes($str);
}
}

Of course, if you have no way of knowing whether your global variable is open, you can also customize a function like this:

Copy code The code is as follows:
function getVar($var)
{
If (ini_set('register_gobals'))
          {
            return $var;
        } else {
            return $_GET['var'];
}
}

Of course, you can use the PHP configuration function ini_get() for many purposes, and you will slowly experience it yourself.

I hope this article will be helpful to everyone’s PHP programming design.

Problems with ini_set() function in PHP

The first parameter is the variable name in the configuration file, and the second parameter is the value to be assigned.
For example, there is such a line in the configuration file: path=''
Then the function of ini_set('path', 'c:\windows') is to change this line to
path='c:\windows '

What is the PHP statement @ini_set('memory_limit', '64M');?

Set the memory size that php can use to 64M.
Generally, the php.ini file cannot be modified on the remote host and can only be set through the program

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906114.htmlTechArticleUsage examples of ini_set and ini_get in PHP, ini_setini_get This article describes the usage of ini_set and ini_get in PHP. Share it with everyone for your reference. The details are as follows: PHP ini_set function. P...
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