Home > Backend Development > PHP Tutorial > A brief discussion on PHP variable scope and address reference issues_PHP tutorial

A brief discussion on PHP variable scope and address reference issues_PHP tutorial

WBOY
Release: 2016-07-13 17:18:35
Original
1059 people have browsed it

The concept of scope:

Variables can be declared anywhere in a PHP script, however, where a variable is declared will greatly affect the scope of access to the variable. This accessible range is called scope.

The main commonly used ones include: local variables, global variables, and static variables.

1. Local variables: They are variables declared within a function. They are stored in the memory stack, so access speed is very fast. Only valid within a function.

2. Global variables: Contrary to local variables, global variables can be accessed anywhere in the program. As long as the keyword GLOBAL is added in front of the variable, it can be recognized as a global variable. Valid within the entire php file.

3. Static variables: Use static to modify variables that only exist in the function scope. Their values ​​will not disappear after the function is executed. Note: It cannot be initialized again after initialization, and expressions cannot be used to assign values.

Copy code The code is as follows:

function test()
{
static $b=0;/ /Declare static variables. If declared outside the function, they will not be used inside the function

$b=$b+1;

echo $b;
}
test();//This statement will output the value of $b as 1
test();//This statement will output the value of $b as 1 2

Note: The assignment operation of static $b=0 will only be executed when the variable is initialized for the first time.

Attachment A: Static members and static methods in a class are almost always called using the class name or self or parent plus: :xxx. Their scope is the same as this, but their declaration is outside the method

Appendix B: The scope in js is: use var aa=‘xxx’; what is declared outside the function is the global variable (regardless of whether it has the modifier var or not). Local variables are declared using var inside a function, and global variables are declared without var.

Attachment C: About citations

PHP reference: Add &. Reference in php before a variable, function or object is to access the contents of the same variable with different names.

1. Variable reference:

Copy code The code is as follows:

$a="ABC";

$b =&$a;

echo $a;//Output here: ABC

echo $b;//Output here: ABC

$b="EFG";

echo $a;//The value of $a here becomes EFG, so EFG is output

echo $b;//Output EFG here

2. Function call by address

Copy code The code is as follows:

function test(&$a)

{

$a=$a+100;

}

$b=1;

echo $b;//output 1

test($b); //What $b is passed to the function here is actually the memory address where the variable content of $b is located. The value of $b can be changed by changing the value of $a in the function

echo "
";

echo $b;//Output 101

3. Function reference return

Copy code The code is as follows:

function &test()
{

static $b=0;//Declare a static variable

$b=$b+1;

echo $b;

return $b;

}

$a=test();//This statement will output that the value of $b is 1

$a=5;

$a=test();//This statement will output that the value of $b is 2

$a=&test();//This statement will output that the value of $b is 3

$a=5;

$a=test();//This statement will output that the value of $b is 6

Analysis: What you get by using $a=test() is not actually a reference return from the function. Just copy the return value of the function to $a without affecting $b. This call is no different from an ordinary call.

Php stipulates: $a=&test() method is the reference return of the function. He pointed the memory address of the $b variable and the memory address of the $a variable to the same place. That is equivalent to $a=&$b;

4. Unquote

Copy code The code is as follows:

$a = 1;

$b =& $a;

unset ($a);

echo $b;

Analysis: unsetting a reference only cancels the binding between the variable name and the content of the variable. It does not mean that the content is destroyed, and its value still exists.

5. Global reference: When using global $var to declare a variable, you actually create a reference to the global variable. Global $val <=> $var=&$GLOBALS[‘var’] ;

6. Object reference: In the object method, the object called by $this is the reference that calls it

Note: The pointing of addresses in PHP is not implemented by the user himself, but through the zend core. PHP references adopt the principle of "write copy", which means that unless a write operation occurs, it points to the same address. Variables or objects will not be copied.

Copy code The code is as follows:

$a = 1;

$b =$a;


$a and $b both point to the same memory address, it is not that $a and $b occupy different memories.

If you execute the sentence $a="dsd" now: the memory data pointed to by $a and $b need to be rewritten, at this time the zend core will automatically judge. Automatically generate a data copy of $a for $b, and re-apply a piece of memory for storage.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621647.htmlTechArticleThe concept of scope: Variables can be declared anywhere in the PHP script, but the location where the variable is declared will greatly Affects the scope of access variables. This accessible range is called a scope...
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