Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of ini_set and ini_get in php

Detailed explanation of usage examples of ini_set and ini_get in php

怪我咯
怪我咯Original
2017-07-06 10:57:281944browse

This article mainly introduces the usage of ini_set and ini_get in PHP, and describes the specific functions and applicability of ini_set and ini_get functions in the form of examples. It has certain reference value. Friends in need can refer to it

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.

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. Specific values ​​​​can be set, you can view the list in the manual.

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

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_reportingTo set the level of error information displayed.

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

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.

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 in the configuration file , but do you have permission to change php.ini, then you can combine the two functions:

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 this 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 a string is returned. string.

For example, the example in the manual:

The code is as follows:

< ?php
echo &#39;display_errors = &#39; . ini_get(&#39;display_errors&#39;) . " "; //显示错误是否打开
echo &#39;register_globals = &#39; . ini_get(&#39;register_globals&#39;) . " ";//全局变量是否打开
echo &#39;post_max_size = &#39; . ini_get(&#39;post_max_size&#39;) . " ";//最多能提交的文件大小
echo &#39;post_max_size+1 = &#39; . (ini_get(&#39;post_max_size&#39;)+1) . " ";
?>

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

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:

The code is as follows:

function stringFilter($str)
{
     if (ini_get(&#39;magic_quotes_gpc)&#39;)
    {
       return $str;
    }else{
       return addslashes($str);
    }
}

Of course, if you can’t know your Whether global variables are turned on, you can also customize such a function: The code is as follows:

function getGetVar($var)
{
       if (ini_set(&#39;register_gobals&#39;))
      { 
          return $var;
       } else {
          return $_GET[&#39;var&#39;];
       }
}

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

The above is the detailed content of Detailed explanation of usage examples of ini_set and ini_get in php. For more information, please follow other related articles on the PHP Chinese website!

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