This article describes the method used by PHP to obtain the value of php.ini variables in Ini_get. Share to everyone for your reference. The specific analysis is as follows:
To get the value of the variable in php.ini, of course, you can use Phpinfo () to all the PHP configuration information, but if you want a variable value, how do you get it?
PHP provides a function to get the value of a variable in php.ini: Ini_get ()
The use of Ini_get () is very simple, and the following examples illustrate how it is used.
Grammar:
String Ini_get (String varname)
The return value is 0 or 1 if the Boolean type
Instance:
<?php
/* We
php.ini contains the following settings:
display_errors = on
register_globals = Off
post_max_size = 8M
*
/echo ' display_errors = '. Ini_get (' display_errors '). "\ n";
echo ' register_globals = '. Ini_get (' register_globals '). "\ n";
echo ' post_max_size = '. Ini_get (' Post_max_size '). "\ n";
echo ' post_max_size+1 = '. (Ini_get (' post_max_size ') +1). "\ n";
Echo ' post_max_size in bytes = '. Return_bytes (Ini_get (' post_max_size '));
function Return_bytes ($val) {
$val = trim ($val);
$last = Strtolower ($val [Strlen ($val)-1]);
Switch ($last) {
//the ' G ' modifier is available since PHP 5.1.0 case
' G ':
$val *= 1024;
Case ' m ':
$val *= 1024;
Case ' K ':
$val *= 1024;
}
return $val;
>
The results of the above code are similar to the following:
display_errors = 1
register_globals = 0
post_max_size = 8M
post_max_size+1 = 9
post_max_size in bytes = 8388608
If you want to get the value of the variable in the entire php.ini, we can use the Ini_get function Ini_get_all ().
The Ini_get_all () function returns the entire PHP environment variable in the form of an array, and is simple to use.
Example one:
<?php
Print_r (Ini_get_all ("Pcre"));
Print_r (Ini_get_all ());
? >
The results of the above code are similar to the following:
Array
(
[pcre.backtrack_limit] => array
(
[global_value] => 100000
[Local_value] => 100000
[Access] => 7
)
[pcre.recursion_limit] => Array
(
[Global_value] => 100000
[Local_value] => 100000
[Access] => 7
)
)
array
([
allow_call_time_pass_reference] => array
(
[ Global_value] => 0
[Local_value] => 0
[Access] => 6
)
[Allow_url_fopen] => Array
(
[Global_value] => 1
[local_value] => 1
[Access] => 4
)
...
)
Example two:
<?php
Print_r (Ini_get_all ("Pcre", false);//Added in PHP 5.3.0
print_r (Ini_get_all (null, FALSE));//Added In PHP 5.3.0
?>
The output is similar to the following:
Array
(
[pcre.backtrack_limit] => 100000
[Pcre.recursion_limit] => 100000
)
array
(
[allow_call_time_pass_reference] => 0
[Allow_url_fopen] => 1
...
)
The function relative to Ini_get () is Ini_set () and Ini_set has the ability to change php.ini settings. For example, when a script runs overtime, it can set its maximum execution time.
I hope this article will help you with your PHP program design.