PHP Get_cfg_var () and Ini_get () are functions that get configuration values, and when you need to get the configuration value of an option in php.ini, both functions are available and the results are the same.
However, there are some differences between Get_cfg_var () and Ini_get (), which is what this article is going to talk about.
Before we tell the difference between these two functions, let's look at their meanings and usage.
For the use of Ini_get (), there is a "PHP using Ini_get to obtain the value of php.ini variables," Here is no longer cumbersome.
The following is mainly about the Get_cfg_var () function.
Get_cfg_var: Gets the configuration option value for PHP.
Syntax: String Get_cfg_var (string varname);
return value: String
Content Description: If the current PHP configuration option varname is correctly obtained, the value of the variable is returned. The failure returns false.
Let's talk about the difference between the two functions.
In fact, the difference between the two functions is very distinct, and easy to learn and easy to use.
Get_cfg_var (): The value taken is the value in the configuration file
Ini_get (): The current value of the fetch
Like what
?
123 |
ini_set ( ‘SMTP‘ , ‘192.160.0.24‘ ); // 改变 SMTP 的当前值 print get_cfg_var( ‘SMTP‘ ); // 返回 localhost print ini_get ( ‘SMTP‘ ); // 返回 192.160.0.24 |
Code
?
123456789101112131415 |
<?php
/*
Our 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"
;
//POST提交内容限制:
echo ‘post_max_size+1 = ‘ . (
ini_get
(
‘post_max_size‘
)+1) .
"\n"
;
echo ‘allow_url_fopen = ‘ . (
ini_get
(
‘allow_url_fopen‘
)) .
"\n"
;
//使用URL打开文件:
?>
|
Output:
?
12345 |
display_errors = 1 register_globals = 0 //在我机器上什么也没有 post_max_size = 8M post_max_size+1 = 9 allow_url_fopen = 1 |
Code
?
123456789101112131415 |
<?php
/*
Our php.ini contains the following settings:
display_errors = On
register_globals = Off
post_max_size = 8M
*/
echo ‘display_errors = ‘ . get_cfg_var(
‘display_errors‘
) .
"\n"
;
echo ‘register_globals = ‘ . get_cfg_var(
‘register_globals‘
) .
"\n"
;
echo ‘post_max_size = ‘ . get_cfg_var(
‘post_max_size‘
) .
"\n"
;
//POST提交内容限制:
echo ‘post_max_size+1 = ‘ . (get_cfg_var(
‘post_max_size‘
)+1) .
"\n"
;
echo ‘allow_url_fopen = ‘ . (get_cfg_var(
‘allow_url_fopen‘
)) .
"\n"
;
//使用URL打开文件:
?>
|
Output
?
12345 |
display_errors = 1 register_globals = post_max_size = 8M post_max_size+1 = 9 allow_url_fopen = 1 |
In addition, here is the Ini_get_all () function, which differs from Ini_get (), where the Ini_get_all () function returns an entire PHP environment variable in the form of an array, although its usage is simple.
Ini_get_all () returns all option values as an array, so you can use them when phpinfo () is not available.
Example:
?
1234 |
<?php $inis = ini_get_all (); print_r( $inis ); ?> |
Output:
?
123456789101112131415 |
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 ) … ) |
I hope this article is helpful to everyone's PHP programming.
Usage and differences of Get_cfg_var () and Ini_get () in PHP