Variable activity range

Source: Internet
Author: User
The activity scope of a variable is limited to defining its context. For the vast majority of PHP variables, there is only a separate scope of activity. However, the concept of a local function range is introduced in user-defined functions. Any variables used in this function are limited to SyntaxHighlighter. all () within the local function range of the function by default ();

The activity scope of a variable is limited to defining its context. For the vast majority of PHP variables, there is only a separate scope of activity. However, the concept of a local function range is introduced in user-defined functions. Any variables used in this function are limited to the local function range of the function by default. For example:
 
$ A = 1;/* global scope */
Function Test (){
Echo $ a;/* reference to local scope variable */}
Test ();
 
This script will not generate any output, because the statement for the variable "$ a" submitted to the local translation is displayed, at the same time, this variable is not specified in its activity range. You can note that this is somewhat different from the C language. in C, global variables are automatically set to available unless otherwise specified in the function. People may accidentally change the value of the global variable, which may cause many problems in the program. In PHP, global variables must be announced in a function if you want to use them in this function. Example:
 
$ A = 1;
$ B = 2;
Function Sum (){
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
 
The above script will output "3 ". Specify the global variables $ a and $ B in the function. any reference to these two variables is specified to this global variable. There is no function to limit the number of global variables.
The second method to accept global variables is to use PHP's special definition array $ GLOBALS, as shown in the following example:
$ A = 1;
$ B = 2;
Function Sum (){
$ GLOBALS ["B"] = $ GLOBALS ["a"] + $ GLOBALS ["B"];
}
Sum ();
Echo $ B;
 
$ GLOBALS array is the name of the variable using "global" as the union array, and the global variable is used as the value of an element in the variable array.


Another important feature of variable activity scope is "static variables ". A static variable only exists in the activity range of the local function, but its value is not lost when the program leaves this range. See the following example:
 
Function Test (){
$ A = 0;
Echo $;
$ A ++;
}
 
Every time this function is called, the variable $ a is set to 0 and "0" is printed, so it is almost useless. The expression "$ a ++" increases the value of the variable, but $ a disappears every time the function exits. To use a count function that does not lose the current calculation, you can set variable $ a to static, as shown in the following example:
 
Function Test (){
Static $ a = 0;
Echo $;
$ A ++;
}
 
Now, every time the Test () function is called, it prints the variable $ a and the added value.
When a function is called recursively, using static variables is an important method. Recursive functions can call their own functions. When writing recursive functions, pay attention to the possible loop definitions. You must have an appropriate method to interrupt this recursive process. The following example shows 10 recursion times:
 
Function Test (){
Static $ count = 0;
$ Count ++;
Echo $ count;
If ($ count <10 ){
Test ();}
$ Count --;
}

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.