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 'display_errors = ' . ini_get('display_errors') . " "; //显示错误是否打开 echo 'register_globals = ' . ini_get('register_globals') . " ";//全局变量是否打开 echo 'post_max_size = ' . ini_get('post_max_size') . " ";//最多能提交的文件大小 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
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('magic_quotes_gpc)') { 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('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 .
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!