The use and distinction _php skills of Get_cfg_var () and Ini_get () in PHP

Source: Internet
Author: User

This article illustrates the usage and difference of get_cfg_var () and Ini_get () in PHP. Share to everyone for your reference. The specific analysis is as follows:

PHP Get_cfg_var () and Ini_get () are the functions to get the configuration value, when you need to get the configuration value of an option in php.ini, both functions can be used, the result is the same.

However, there are some differences between Get_cfg_var () and Ini_get (), which is what this article is about.

Before we say the difference between these two functions, let's take a look at their meanings and usage.

For the use of Ini_get (), there is a "method of PHP using Ini_get to get the value of variables in PHP.ini," which is no longer burdensome.

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 correct access to the current PHP configuration options varname, then return the value of the variable. Returns False if it fails.

Let's say the difference between the two functions

In fact, the difference between the two functions is very clear, and easy to learn and ease of use.

Get_cfg_var (): value is the value in the configuration file
Ini_get (): The current value is taken

Like what

Ini_set (' SMTP ', ' 192.160.0.24 '); Change the current value of SMTP
print Get_cfg_var (' SMTP ');//return localhost
print ini_get (' SMTP ');//Back to 192.160.0.24

Code

<?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";
Post commit content limit:
echo ' post_max_size+1 = '. (Ini_get (' post_max_size ') +1). "\ n"; 
echo ' Allow_url_fopen = '. (Ini_get (' Allow_url_fopen ')). "\ n";
To open a file using a URL:
?>

Output:

display_errors = 1
register_globals = 0//Nothing on my machine
post_max_size = 8M
post_max_size+1 = 9
allow_ Url_fopen = 1

Code

<?php
/* We
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 commit 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";
To open a file using a URL:
?>

Output

display_errors = 1
register_globals =
post_max_size = 8M
post_max_size+1 = 9
allow_url_fopen = 1

Also, here is a reference to the Ini_get_all () function, which, unlike Ini_get (), is a ini_get_all () function that returns the entire PHP environment variable as an array, but its usage is simple.

Ini_get_all () returns all the option values as an array, which you can use when phpinfo () is not available.

Example:

<?php 
$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] => 1 
[Access] => 7 
) 
...
)

I hope this article will help you with your PHP program design.

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.