Get_cfg_var() and ini_get() in php are both functions for obtaining configuration values. When you need to obtain the configuration value of an option in php.ini, both functions can be used, and the results obtained are the same. .
However, there are some differences between get_cfg_var() and ini_get(), which is what this article will talk about.
Before talking about the difference between these two functions, let’s first understand their meaning and usage.
Regarding the usage of ini_get(), I already wrote a blog post yesterday "How to use ini_get to get the variable value in php.ini", so I won’t go into details here.
The following mainly talks about the get_cfg_var() function.
get_cfg_var
Get the PHP configuration option value.
Syntax: string get_cfg_var(string varname);
Return value: String
Content description: If the current PHP configuration option varname is obtained correctly, the variable value will be returned. Returns false on failure.
Let’s talk about the difference between these two functions
In fact, the difference between these two functions is very clear, and they are easy to understand, learn and use.
get_cfg_var(): The value taken is the value in the configuration file
ini_get(): The current value taken
For example
ini_set('SMTP', '192.160.0.24'); // Change the current value of SMTP
print get_cfg_var('SMTP'); // Return to localhost
print ini_get('SMTP'); // Return 192.160.0.24
code
/*
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 submission content limit:
echo 'post_max_size+1 = ' . (ini_get('post_max_size')+1) . "n";
echo 'allow_url_fopen = ' . (ini_get('allow_url_fopen')) . "n";//Open the file using URL:
?>
Output:
display_errors = 1
register_globals = 0//On my machine Nothing
post_max_size = 8M
post_max_size+1 = 9
allow_url_fopen = 1
code
/*
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 submission content limit:
echo 'post_max_size+1 = ' . (get_cfg_var('post_max_size')+1) . "n";
echo 'allow_url_fopen = ' . (get_cfg_var(' allow_url_fopen')) . "n";//Open the file using URL:
?>
output
display_errors = 1
register_globals =
post_max_size = 8M
post_max_size+1 = 9
allow_url_fopen = 1
Also, here Let me mention the ini_get_all() function. This function is different from ini_get(). The ini_get_all() function returns the entire PHP environment variables in the form of an array, but its usage is also very simple.
ini_get_all() will return all option values in the form of an array, which is convenient for you to use when phpinfo() cannot be used.
Example:
$inis = ini_get_all();
print_r($inis);
?>
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] => [access] => 7
)
…
)
The above has introduced examples to illustrate the usage and difference between get_cfg_var() and ini_get(), including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.