Several PHP functions related to PhP. ini

Source: Internet
Author: User
Tags php website

PHP provides four functions for PHP. ini operations: ini_get, ini_set, ini_get_all, ini_restore
The most common ones are ini_set and ini_get.
The functions of these two functions are described as follows:
Ini_get ()

Obtain the option value of the configuration file, that is, obtain the value of an option in the configuration file. If it is true, 1 is returned. If it is false, 0 is returned, and a string is returned.
For example:
<? PHP

Echo 'display _ errors = '. ini_get ('display _ errors'). "N"; // whether the Display error is enabled
Echo 'register _ globals = '. ini_get ('register _ globals'). "N"; // whether the global variable is enabled
Echo 'Post _ max_size = '. ini_get ('Post _ max_size'). "N"; // maximum file size that can be submitted
Echo 'Post _ max_size + 1 = '. (ini_get ('Post _ max_size') + 1). "N ";
?>
Output:
Display_errors = 1
Register_globals = 0
Post_max_size = 8 m
Post_max_size + 1 = 9
The magic_quotes_gpc = on option is provided in PHP. ini settings to automatically escape the variable values obtained from post and get.
PHP also provides the addslashes () function to accomplish the same function. Therefore, when we write the filter function, to be compatible with previous versions (I have not checked which PHP version supports the magic_quotes_gpc option) or to determine whether this setting is enabled, to determine whether to escape the get and post variables in the program, you can write a function as follows:

Function stringfilter ($ Str)
{
If (ini_get ('Magic _ quotes_gpc )'){
Return $ STR;
} Else {
Return addslashes ($ Str );
}
}
Another common case is that if you do not know whether the global variable in PHP. INI is enabled, you can write a function like this:

Function getgetvar ($ var)
{
If (ini_set ('register _ gobals ')){
Return $ var;
} Else {
Return $ _ Get ['var'];
}
}
The above are just two common examples to illustrate the usefulness of the ini_get () function, and can be used elsewhere.
Ini_set ()
Set PHP. some variable values in ini, that is, PHP. value in an option in ini (note that this setting takes effect after the function is executed and fails when the script ends, and not all options can be changed to function settings, reference manual)
For example, if the display_error option in PHP. INI is disabled, but you want to display the error information in the program to facilitate program debugging, you can use this function:
Ini_set ("display_errors", "on ");
Of course there are other settings, depending on the requirements you use to control these options;
There is a flexible usage that can be seen on the Internet. For example, if you want to add your own include file path to the configuration file, but do you have the permission to change PHP. INI, you can combine two functions:
Ini_set ('include _ path', ini_get ('include _ path'). ':/your_include_dir :');
Two other uncommon Functions
Ini_get_all ()
Obtain all the set Option variables, that is, return all option values in the form of arrays, so that you can use them when phpinfo () is unavailable.
Example of a manual, for example:
<? 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 ()
Reply to the default value of the configuration file. After ini_set is used, you can use it to restore the value before set.
---------------------------
Appendix: a more detailed PHP website w3school
Http://www.w3school.com.cn/php/index.asp

In addition, PhP5 provides a set of functions to filter variables.

* Filter_var ()-filter a single variable using a specified filter
* Filter_var_array ()-filter multiple variables using the same or different filters
* Filter_input-gets an input variable and filters it.
* Filter_input_array-obtains multiple input variables and filters them using the same or different filters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.