A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
What is the reference?
Referencing in PHP means accessing the contents of the same variable with a different name. This is not like a C pointer, but instead, the reference is a symbolic table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be viewed as hardlink in Unix file systems.
What does the reference do
PHP references allow two variables to point to the same content. It means that when you do this:
This means that $a and $b point to the same variable.
$a and $b are exactly the same here, not $a pointing to $b or vice versa, but $a and $b pointing to the same place.
If an array with a reference is copied, its value is not lifted. The same is true for arrays passing values to functions.
If a reference assignment to an undefined variable, a reference parameter pass, or a reference return is made, the variable is created automatically.
Example #1 Use a reference to an undefined variable
The same syntax can be used in functions, which return references and are used in the new operator (PHP 4.0.4 and later versions):
Since PHP 5, new automatically returns references, so using =& here is obsolete and generates E_STRICT-level messages.
The use of the & operator causes the object to generate a copy. If $this in a class, it will be used for the current instance of the class. It is not always the desired result to copy this instance (such as objects) without assigning the & and $this will work on the copy. Because of performance and memory consumption problems, you usually just want to work on one instance.
Although you can use the @ operator to suppress any error information in a constructor, such as with a @new, this does not work with the &new statement. This is a limitation of the Zend engine and can cause a parse error.
If you assign a reference to a variable declared as global within a function, the reference is visible only inside the function. You can avoid this by using a $GLOBALS array.
Example #2 Reference global variables within a function
$var the global; As $var =& $GLOBALS [' var ']; 's Shorthand. Thus assigning other references to the $var changes only the references to local variables.
If you assign a value to a variable that has a reference in a foreach statement, the referenced object is also changed.
Example #3 References and foreach statements
The second thing a reference does is to pass a variable with a reference. This is done by creating a local variable within the function and referencing the same content within the call scope. For example:
$a = 5;
will make $a into 6. This is because the variable $var points to the same content that the $a points to in the Foo function. See the reference pass for more detailed explanations.
The third thing a reference does is a reference return.
References are not
As mentioned earlier, references are not pointers. This means that the following structure does not produce the desired effect:
This will cause the $var variable in the Foo function to be bound to the $bar at the time of the function call, but then to be rebind to the $GLOBALS ["Baz"]. It is not possible to bind a $bar to another variable in the scope of a function call through a reference mechanism, because there is no variable $bar in the function foo (it is represented as $var, but $var only the variable content but not the binding of the name to the value in the symbol table). You can use reference returns to refer to variables that are selected by the function.
A variable can be passed to a function by reference, so that the function can modify the value of its arguments. The syntax is as follows:
Note that there is no reference symbol in the function call--only in the function definition. The function definition alone is sufficient to pass the argument correctly by reference. In the most recent version of PHP if the & used in Foo (& $a); Will get a warning that "Call-time pass-by-reference" is out of date.
The following can be passed by reference:
variables, such as foo ($a)
New statement, such as Foo (new Foobar ())
The reference returned from the function, for example:
See the reference return for a detailed explanation.
No other expression can be passed by reference and the result is undefined. For example, the following reference pass example is invalid:
These conditions are available in PHP 4.0.4 and later versions.
Reference return is used when you want to use a function to find the reference to which variable it should be bound. Do not use return references to increase performance, the engine is smart enough to optimize itself. Return the reference only if there is a reasonable technical reason! To return a reference, use this syntax:
In this example, the properties of the object returned by the GetValue function are assigned, not copied, as if they were not referenced by reference syntax.
Note: Unlike parameter passing, there must be a & symbol in all two places--it indicates that the return is a reference rather than a common copy, and also that $myValue is a reference binding rather than a normal assignment.
Note: If you try to return a reference from a function like this: Returns ($this->value), this will not work because you are trying to return the result of an expression rather than a referenced variable. You can only return reference variables from a function--no other way. If the code attempts to return the result of a dynamic expression or the new operator, a e_notice error is emitted from PHP 4.4.0 and PHP 5.1.0.
$a =test () way to call a function, just the value of the function to $a only, and $a do any change will not affect the $b in the function, and through the $a=&test () way to call the function, and his role is to return $b the $b variable in the memory address and $ The memory address of a variable points to the same place, which produces the equivalent effect ($a =&b;) So changing the value of the $a also changes the value of the $b, so in the execution of the $a =&test (); $a = 5; Later, the value of the $b changed to 5.
When unset a reference, only the binding between the variable name and the variable content is disconnected. This does not mean that the variable content has been destroyed. For example:
Not unset $b, just $a.
Taking this analogy with Unix's unlink may help to understand.
Many of the syntax constructs of PHP are implemented by reference mechanisms, so all of the above about reference bindings apply to these constructs as well. Some structures, such as reference passing and return, have been mentioned above. Other structures that use references are:
When declaring a variable with global $var, a reference to a global variable is actually established. In other words, it is the same as doing this:
This means, for example, that unset $var does not unset global variables.
The results of using unset ($a) and $a=null are not the same. If the block memory has only $a a map, then unset ($a) is equivalent to $a=null, the reference count of the memory becomes 0 and is automatically reclaimed, and if the block memory has $a and $b two mappings, then unset ($a) will cause $a=null and $b the same situation, $a= Null can cause $a= $b =null.
Reason: A variable assignment is null, which causes the reference count of the memory block corresponding to the variable to be set directly to 0 and automatically recycled.
In the method of an object, $this is always a reference to the object that called it.
The role of a reference
If the program is larger, the number of references to the same object is more, and you want to use the object to remove it manually, personal advice to use the "&" mode, and then clear the $var=null. At other times, use the PHP5 default method. In addition, in PHP5 for large array of transmission, it is recommended to use the "&" mode, after all, save memory space use.
Here's a little episode. The pointer to address (like pointers) in PHP is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "write-time copy" principle, unless a write operation, point to the same address variables or objects are not copied.
In layman's terms.
1: If you have the following code
In fact at this point, $a and $b are pointing to the same memory address, rather than $a and $b occupy different memory.
2: If you add the following code based on the above code
Since $a and $b point to the memory of the data to be written again, at this time the Zend core will automatically determine the $b production of a $a copy of the data, reapply a piece of memory for storage.
The above is about the full content of PHP reference, I hope you can enjoy.
Start building with 50+ products and up to 12 months usage for Elastic Compute Service