Example of ini_set and ini_get usage in PHP

Source: Internet
Author: User
This article mainly introduces the usage of ini_set and ini_get in PHP, describes the specific functions and applicability of ini_set and ini_get functions in the form of instances, and has some reference value, for more information about how to use ini_set and ini_get in PHP, see the following example. Share it with you for your reference. The details are as follows:

PHP ini_set function.

The PHP ini_set function is the value in the setting option. it takes effect after the function is executed and expires when the script ends. Not all options can be set by this function. You can set specific values to view the list in the manual.

You can set the option value in php. ini. for example, if the display_error option is disabled, but you want to display the error information in the program so that you can debug the program, you can use the PHP ini_set function:

The code is as follows:

Ini_set ("display_errors", "On ");

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

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 is changed from 30 seconds to 180 seconds by default. of course, you can also set it using set_time_limit.

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

The code is as follows:

Ini_set ('include _ path', ini_get ('include _ path'). ':/your_include_dir :');



Functions in PHP have different functions. you can use these functions flexibly to play a major role in this language.

The PHP configuration function ini_get () can be used by many people to obtain the value of an option in the configuration file. if it is true, 1 is returned. if it is false, 0 is returned, returns the string.

For example, in the manual:

The code is as follows:

<? Php
Echo 'display _ errors = '. ini_get ('display _ errors'). "; // whether the display error is enabled
Echo 'register _ globals = '. ini_get ('register _ globals'). ""; // whether the global variable is enabled
Echo 'post _ max_size = '. ini_get ('post _ max_size'). ""; // 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 = 8 M
Post_max_size + 1 = 9

The PHP configuration function ini_get () is mainly used to obtain the configuration file, which facilitates many operations. For example, if you want to filter strings, but do not know whether magic_quotes_gpc is enabled, 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 cannot know whether your global variables are enabled, you can also customize the following functions:
  

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.

I hope this article will help you with PHP programming.

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.