Home > Backend Development > PHP Tutorial > Active scope of variables_PHP tutorial

Active scope of variables_PHP tutorial

WBOY
Release: 2016-07-13 17:26:56
Original
878 people have browsed it

The scope of a variable is limited to the context in which it is defined. There are only single scopes for all PHP variables in most sections. However, in user-defined functions, a concept of local function scope is introduced. Any variables used in this function are by default restricted to the function's local scope. For example:
 
$a = 1; /* global scope */
Function Test () {
echo $a; /* reference to local scope variable */ }
Test () ;

This script will not cause any output, because the declaration of the variable "$a" is submitted to the local translation, and this variable is not assigned a value in its active scope. You can notice that this is a little different from C, where global variables are automatically made available unless otherwise specified in the function. This can cause many problems in a program because people can accidentally change the value of a global variable. In PHP global variables must be declared in a function if you want to use it in this function. Examples are as follows:

$a = 1;
$b = 2;
Function Sum () {
global $a, $b;
$b = $a + $ b;
}
Sum ();
echo $b;
 
The above script will output "3". Global variables $a and $b are declared in the function, and any references to these two variables are assigned to the global variables. There is no limit on the number of global variables that functions can operate on.
The second way to accept global variables is to use PHP's special definition array $GLOBALS. The example is as follows:
$a = 1;
$b = 2;
Function Sum () {
$GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
}
Sum ();
echo $b;
 
The $GLOBALS array is an associative array using "global" as the name of the variable, and the global variable is used as the value of an element in the variable array.


Another important feature regarding the scope of variables is "static variables". A static variable only exists in the active scope of the local function, but its value is not lost when the program leaves this scope. Please refer to the following example:

Function Test () {
$a = 0;
echo $a;
$a++;
}

This function Each time it is called, the variable $a is set to 0 and "0" is printed, so it is almost useless. The expression "$a++" will increment the value of the variable, but the variable $a disappears every time the function exits. To use a counting function without losing the current calculation, the user can set the variable $a to static, as shown in the following example:
 
Function Test () {
static $a = 0;
echo $a;
$a++;
}

Now, every time the Test() function is called, it will print out the variable $a and the value after it was incremented.
When a function is called recursively, using static variables is a very important method. A recursive function is a function that can call itself. When writing recursive functions, you must be aware of possible loop definitions. You must have a proper way to interrupt this recursive process. The following example recurses 10 times:

Function Test () {
static $count = 0;
$count++;
echo $count;
if ($count Test (); }
$count--;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531939.htmlTechArticleThe scope of activity of a variable is limited only to the context in which it is defined. There are only single scopes for all PHP variables in most sections. However, in user-defined functions, quote...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template