This article introduces the scope, reference, object reference, and transfer of php variables. For more information, see.
Variable range
The scope of a variable is the context defined by it (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require. For example:
The Code is as follows: |
Copy code |
<? Php $ A = 1; Include 'B. inc '; ?>
|
The variable $ a will take effect in the included file B. inc. However, in user-defined functions, a local function range will be introduced. By default, any variable used in a function is limited to a local function. For example:
The Code is as follows: |
Copy code |
<? Php $ A = 1;/* global scope */ Function Test () { Echo $ a;/* reference to local scope variable */ } Test (); ?> |
This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. You may notice that the global variables in PHP are a little different from those in C language. In C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. Some may accidentally change a global variable. Global variables in PHP must be declared as global when used in functions.
The reference in PHP means that different names access the same variable content.
The pointer in the C language is different from the pointer in the C language. The pointer in the C language stores the address where the variable content is stored in the memory.
Variable reference
The Code is as follows: |
Copy code |
<? Php $ A = "ABC "; $ B = & $; Echo $ a; echo "rn"; // output here: ABC Echo $ B; echo "rn"; // output here: ABC $ B = "EFG "; Echo $ a; echo "rn"; // here, the value of $ a is changed to EFG, so EFG is output. Echo $ B; echo "rn"; // output EFG here ?>
|
Function address transfer call
I will not talk much about the address transfer call. The following code is provided directly.
The Code is as follows: |
Copy code |
<? Php Function test (& $) { $ A = $ a + 100; } $ B = 1; Echo $ B; // output 1 Echo "<br/> "; Test ($ B); // here, $ B Actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function. Echo "<br/> "; Echo $ B; // output 101 ?>
|
Note that test (1); will cause an error.
Function reference return
First look at the code
The Code is as follows: |
Copy code |
<? Php Function & test () { Static $ B = 0; // declare a static variable $ B = $ B + 1; Echo $ B; Return $ B; } $ A = test (); // This statement outputs the value of $ B as 1. Echo "<br/> "; $ A = 5; $ A = test (); // This statement outputs the value of $ B to 2. Echo "<br/> "; $ A = & test (); // This statement outputs the value of $ B to 3. Echo "<br/> "; $ A = 5; $ A = test (); // This statement outputs a value of 6 for $ B. ?> |
The following explains:
In this way, $ a = test (); does not actually get the function reference and return, which is no different from the normal function call. The reason is: this is the php rule.
Php requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the php manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.
The example above is as follows:
$ A = test () is used to call a function. It only assigns the value of the function to $ a. Any change made to $ a does not affect $ B in the function.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B Variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, the value of $ B is changed to 5.
Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.
Object Reference
The Code is as follows: |
Copy code |
<? Php Class { Public $ abc = "ABC "; } $ B = new; $ C = $ B; Echo $ B-> abc; // output ABC here Echo $ c-> abc; // output ABC here $ B-> abc = "DEF "; Echo $ c-> abc; // output DEF here ?> |
The above code is the running effect in PHP5
In PHP5, object replication is implemented through reference. In the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B;
In PHP5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone.
For example
The Code is as follows: |
Copy code |
<? Php Class { Public $ abc = "ABC "; } $ B = new; $ C = $ B; $ D = clone $ B; Echo $ B-> abc; // output ABC here Echo $ c-> abc; // output ABC here $ B-> abc = "DEF "; Echo $ c-> abc; // output DEF here Echo $ B-> abc; // output DEF here $ D-> abc= "111 "; Echo $ d-> abc; // output 111 Echo $ B-> abc; // output DEF here, indicating that the change of the cloned copy $ d object does not affect $ B object ?> |
Role of reference
If the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then clear it in the form of $ var = null. in other cases, use the default php5 method. in addition, in php5
We recommend that you use the "&" method to transfer large arrays, which saves memory space.
Cancel reference
When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:
The Code is as follows: |
Copy code |
<? Php $ A = 1; $ B = & $; Unset ($ ); Var_dump ($ a); // null is output here Var_dump ($ B); // int 1 is output here ?> |
Not unset $ B, just $.
Global Reference
When a variable is declared with global $ var, a reference to the global variable is actually created. That is to say, it is the same as doing so:
$ Var = & $ GLOBALS ["var"];
?>
This means that, for example, unset $ var does not unset global variables.
$ This
In the method of an object, $ this is always a reference to the object that calls it.
// Next is an episode
In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in php, the reference uses the principle of "Copy at write, that is, unless a write operation occurs, the variable or object pointing to the same address is
It will not be copied.
In layman's terms
1: If the following code exists:
$ A = "ABC ";
$ B = $;
In fact, both $ a and $ B point to the same memory address, not $ a and $ B occupy different memory.
2: Add the following code on the basis of the above Code:
$ A = "ABC ";
$ B = $;
$ A = "EFG ";
Because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core automatically determines that $ B will generate a $ a data copy, apply for a new memory for storage
Variable transfer for php learning!
1. Use url to pass variables
Instance
The Code is as follows: |
Copy code |
<Html> <Head> <Title> my favorite moviesite </title> </Head> <Body> <? Php Echo "my favorite movie site is :"; Echo $ _ GET ['favmovie ']; Echo "</br> "; $ Movierate = 5; Echo "my favorite movie rating for this movie is "; Echo $ movierate; ?> </Body> </Html> |
Save this file as movie1.php.
Use $ _ GET ['favmovie '] to receive the variable passed by the url!
Write another file and save it
Moviesite. php
The Code is as follows: |
Copy code |
<Html> <Head> <Title> find my favorite movie </title> </Head> <Body> <? Php // $ Myfavmovie = urlencode ("life of brian "); Echo "<a href =" moviesite. php? Favmovie = strips "> "; Echo "click here to see more information about my favorite movie! "; Echo "</a> "; ?> </Body> |