PHP global variable definition some usage details _php tutorial

Source: Internet
Author: User
PHP's resolution to the global variable is that global variables are defined globally, but this global variable is not applied to the entire site, but is applied to the current page, including all files of the include or require.


Cases

The code is as follows Copy Code

$a = 1;
$b = 2;
function Sum ()
{
Global $a, $b; Declare it as a global variable inside
$b = $a + $b;
}
Sum ();
Echo $b;
?>

Results: 3
If there is no global variable in the method, global cannot get a $ A, $b value, so in the method you want to use the outside of the variable must first declare that the variable is a global variable, so you can use the

Summary: The PHP global variable defined in the function body can be used outside the body, and the global variable defined outside the function body cannot be used within the function body.

$glpbal $a; $a = 123; function f () {echo $a;//Error,}

Take a look at the following example

The code is as follows Copy Code

function f ()
{
Global $a;
$a = 123;
}
f ();
echo $a; Correct, you can use

In a user-defined function, a local function scope is introduced. Any variables that are used inside the function will be limited to the local function (including variables within the import file of include and require) by default!
Explanation: The internal test_global of the a.php file is a defined third-party function that imports $ A in the global global variable of $ A in the b.php file with include, so $ A is limited to the Test_global local function, so the b.php file is within the $ The scope of a is within the test_global, not the whole a.php ....
Solution:
1. Punch out the local function
a.php file

The code is as follows Copy Code

function Test_global ()
{
Test ();
}
Include ' b.php '; To remove an include from a local Test_global function
$a = 0;
Test_global ();
echo $a;
?>
b.php file
function Test ()
{
Global $a;
$a = 1;
}
?>

2. Excellent access to the viewer

The code is as follows Copy Code

a.php file
Include ' b.php ';
$a = 0;
Set_global ($a);
echo $a;
?>
b.php file
Function Set_global (& $var)
{
$var = 1;
}
?>

Take a look at $globals[]

The code is as follows Copy Code


$var 1 = 1;
$var 2 = 2;
function Test1 () {
$GLOBALS [' var2 '] = & $GLOBALS [' var1 '];
}
Test1 ();
echo $var 2; 1

$var 3 = 1;
$var 4 = 2;
function Test2 () {
Global $var 3, $var 4;
$var 4 = & $var 3;
}
Test2 ();
Echo $var 4;

2 Why does the $VAR2 print result is 1, and $VAR4 's print result is 2? This is because $VAR3 's reference points to the $VAR4 's reference address. The actual value of $var 4 has not changed. The official explanation is that $GLOBALS [' var '] is the external global variable itself, and the global $var is an external $var reference or pointer to the same name.

Perhaps this example is not very clear, I will introduce another example:

The code is as follows Copy Code


$var 1 = 1;
function Test1 () {
unset ($GLOBALS [' var1 ']);
}
Test1 ();
Var_dump ($var 1); Null

$var 2 = 1;
function Test2 () {
Global $var 2;
Unset ($var 2);
}
Test2 ();

echo $var 2; The value of 1 $var 1 is deleted, and the value of $VAR2 is still present. This proves that $var 2 is only an alias reference, and the value of itself is not changed by any means. This means that the global $var is actually $var = & $GLOBALS [' var '], calling an alias of an external variable!

http://www.bkjia.com/PHPjc/631248.html www.bkjia.com true http://www.bkjia.com/PHPjc/631248.html techarticle PHP's resolution to the global variable is that global variables are defined globally, but this global variable is not applied to the entire Web site, but is applied to the current page, including all the text of the include or require ...

  • 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.