In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the scope of PHP variables and the problem of address reference". In daily operation, I believe many people have doubts about how to understand the scope of PHP variables and the problem of address reference. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to understand the scope of PHP variables and the problem of address reference". Next, please follow the editor to study!
The concept of scope:
Variables can be declared anywhere in the PHP script, but where they are declared can greatly affect the scope of the variables being accessed. This accessible scope is called a scope.
The main commonly used variables include: local variables, global variables, static variables.
1. Local variables: variables declared in the function, which are stored in the memory stack, so the access speed is very fast. Valid only within the function.
2. Global variables: in contrast to local variables, global variables can be accessed anywhere in the program. As long as you precede the variable with the keyword GLOBAL, you can recognize it as a global variable. Valid throughout the php file.
3. Static variables: use static to modify variables that only exist in the scope of the function, and their values do not disappear after the execution of the function. Note: after initialization, it cannot be initialized again and cannot be assigned with an expression.
The copy code is as follows:
Function test ()
{
Static $broom0ram / declare static variables, if declared outside the function, it is not needed inside the function
$b=$b+1
Echo $b
}
Test (); / / this statement outputs the value of $b as 1.
Test (); / / this statement outputs a value of $b of 2.
Note: the assignment of static $bread0 will only be performed when the variable is initialized for the first time.
Attachment A: static members and static methods in a class are almost called using the class name or self or parent plus:: xxx. Their scope is the same as this, but his declaration is external to the method.
The scope in the attached B:js makes: what is declared outside the function with var aa='xxx'; is the global variable (with or without the modifier var). Local variables are declared using var inside the function, and global variables are not decorated with var.
Annex C: about citation
PHP reference: to add a reference in & .php to a variable, function, or object is to access the contents of the same variable under a different name.
1. Reference to the variable:
The copy code is as follows:
$a = "ABC"
$b = & $a
Echo $a tincture / output here: ABC
Echo $bterracer / output here: ABC
$b = "EFG"
Echo $a * / the value of $a here changes to EFG, so output EFG
Echo $b _ EFG / output here
2. Address call of the function
The copy code is as follows:
Function test ($a)
{
$a=$a+100
}
$bill1
Echo $bechilink / output 1
Test ($b); / / what $b passes to the function 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 $an in the function.
Echo "
"
Echo $bincinca / output 101
3. The function reference returns
The copy code is as follows:
Function & test ()
{
Static $boun0ram / declare a static variable
$b=$b+1
Echo $b
Return $b
}
$a=test (); / / this statement outputs the value of $b as 1
$axi5
$a=test (); / / this statement outputs the value of $b as 2
$a=&test (); / / this statement outputs the value of $b as 3
$axi5
$a=test (); / / this statement outputs the value of $b as 6
Parsing: what you get with $a=test () is not really the return of a function reference. Just copy the return value of the function to $a without affecting $b. This call is no different from a normal call.
Php states that $a=&test () is the only way to get the function's reference return. He points the memory address of the $b variable to the same place as the memory address of the $a variable. That is to say, it is equivalent to $a million
4. Dereference
The copy code is as follows:
$a = 1
$b = & $a
Unset ($a)
Echo $b
Parsing: unset is a reference that only unbinds the variable name and its content, does not mean that the content is destroyed, and its value is still real.
5. Global reference: when you declare a variable with global $var, you actually create a reference to the global variable. Global $val $var=&$GLOBALS ['var']
6. object reference: in the object's method, the object called by $this is the reference that calls it.
Note: the pointing of the address in php is not realized by the user, but through the core of zend. The reference of php adopts the principle of "write copy", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied.
The copy code is as follows:
$a = 1
$b = $a
Both $an and $b point to the same memory address, not that $an and $b occupy different memory.
If you now execute the sentence $a = "dsd": the memory data pointed to by $an and $b needs to be rewritten, the zend core will automatically determine. Automatically generate a data copy of $a for $b and rerequest a piece of memory for storage.
At this point, the study on "how to understand the scope of PHP variables and the problem of address reference" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.