PHP variable scope (global variable & local variable) & Global & static keyword usage example analysis

Time:2021-10-4

This article describes the usage of PHP variable scope (Global & local variables) & Global & static keyword. Share with you for your reference, as follows:

We know that variables are actually equivalent to the containers we use to store information. I believe you are quite clear about its naming rules, so I won’t repeat them here. Here, we mainly look at the two scopes of PHP variables, global variables & local variables.

Seeing the scope of these two variables, I believe you will come up with two words global & static in your mind. Yes, that’s the two words.

We know that variables defined outside all functions have global scope. In addition to functions, global variables can be accessed by any part of the script. To access a global variable in a function, you need to use the global keyword. However, variables declared inside PHP functions are local variables and can only be accessed inside functions. Let’s take an example:

<?php
$x=5; //  global variable
function myTest()
{
  $y=10; //  local variable
  Echo "< p > test function internal variable: < p >";
  Echo "variable x is: $X";
  echo "<br>";
  Echo "variable y is: $Y";
}
myTest();
Echo "< p > test function external variable: < p >";
Echo "variable x is: $X";
echo "<br>";
Echo "variable y is: $Y";
?>

In the above example, the mytest() function defines the $X and $y variables$ The X variable is declared outside the function, so it is a global variable, and the $y variable is declared inside the function, so it is a local variable.

When we call the mytest() function and output the values of two variables, the function will output the value of the local variable $y, but cannot output the value of $X, because the $X variable is defined outside the function and cannot be used inside the function. If you want to access a global variable in a function, you need to use the global keyword.

Then we output the values of two variables outside the mytest() function. The function will output the value of the global local variable $X, but cannot output the value of $y, because the $y variable is defined in the function and belongs to a local variable.

From this, we can know that we can use the same variable name in different functions, because the variable names defined in these functions are local variables and only act on this function. This is a little trick.

Without much nonsense, let’s look at the global keyword first. The global keyword is used to access global variables within a function. To call the global variables defined outside the function inside the function, we need to add the global keyword before the variables in the function to see an example:

<?php
$x=5;
$y=10;
function myTest()
{
  global $x,$y;
  $y=$x+$y;
}
myTest();
echo $y; //  Output 15
?>

PHP stores all global variables in an array called $globals [index]. Index saves the name of the variable. This array can be accessed inside the function or directly used to update global variables. Therefore, the above example can be written as follows:


<?php
$x=5;
$y=10;
function myTest()
{
  $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
}
myTest();
echo $y;
?>

OK, let’s look at the static keyword.

When a function completes, all its variables are usually deleted. However, sometimes we want a local variable not to be deleted. To do this, we need to use the static keyword when we declare variables for the first time. Let’s take an example:

<?php
function myTest()
{
  static $x=0;
  echo $x;
  $x++;
}
myTest();
myTest();
myTest();// Run result: 0 1 2
?>

We can see that each time the function is called, the variable will retain the value of the previous call. But here we need to know that this variable is still a local variable of the function and cannot be used elsewhere. However, when we use recursion, we still use the static keyword. If you are interested, you can study it a little. It’s still interesting.

Through the above points, there are the following conclusions:

1. What is defined outside the function is a global variable, whose scope is from the definition to the end of the file.
2. A variable defined in a function is a local variable, and its scope is within the scope of the function definition.
3. There are scopes between functions that do not affect each other.
4. The global keyword or $globals [index] array is required to access global variables in the function

In PHP, functions have independent scopes, so local variables will overwrite global variables. Even if there are no variables with the same global variables defined in local variables, they will be overwritten. As follows:

<?php
$a=5;
$b=3;
function t()
{
  echo $a-$b; //  Output 0
}
t();
?>

To use global variables directly in functions, you can declare them through the global keyword or get them through the customized $globals array in PHP:

<?php
$a=5;
$b=3;
function t1()
{ 
  global $a,$b;
  echo $a-$b; //  Output 2
}
t1();
echo PHP_EOL;
function t2()
{
  echo $GLOBALS['a']-$GLOBALS['b']; //  Output 2
}
t2();
?>

Well, that’s it this time.

For more information about PHP, readers who are interested can see the topics on this site: summary of PHP common functions and techniques, summary of PHP string usage, complete collection of PHP array operation skills, tutorial on PHP data structure and algorithm, and summary of PHP programming calculation method

I hope this article will help you in PHP programming.