PHP quotes (&) detailed _php tips

Source: Internet
Author: User
Tags zend
References in PHP mean that different names access the same variable content.
There is a difference between the pointers in the C language. The pointer in C language stores the address of the variable's contents in memory
references to variables
PHP references allow you to use two variables to point to the same content
Copy Code code as follows:

?
$a = "ABC";
$b =& $a;
echo $a//here output: ABC
echo $b//Here Output: ABC
$b = "EFG";
echo $a//The value of $a here becomes EFG so output EFG
echo $b//Here output EFG
?>

call to an address of a function
I'm not going to say more than that. The following directly gives the code
Copy Code code as follows:

Function test (& $a)
{
$a = $a +100;
}
$b = 1;
echo $b;//Output 1
Test ($b); Here $b passed to the function is actually the $b variable content of the memory address, by changing the value of $a in the function can change the value of $b
echo "<br>";
echo $b;//Output 101

Note that in this case test (1), there will be an error, the reason to think for yourself
The function's reference returns
Look at the code first
Copy Code code as follows:

function &test ()
{
Static $b =0;//declares a statically variable
$b = $b +1;
Echo $b;
return $b;
}
$a =test ()//This statement will output $b value of 1
$a = 5;
$a =test ()//This statement will output $b value of 2
$a =&test ()//This statement will output $b value of 3
$a = 5;
$a =test ()//This statement will output $b value of 6

The following explains:
$a=test in this way (); What you get is not the return of a reference to a function, which is no different from a normal function call. As for the reason: this is the PHP rule
PHP rules through $a=&test (); The way to get the function is to return the reference
As for what is the reference return (PHP manual says: Reference returns used when you want to use a function to find the reference should be bound to which variable above.) This shit is killing me for half a day.
The example above is to explain
Calling a function $a =test () simply assigns the value of the function to $a, and $a does nothing to affect the $b in the function
Instead of calling the function by $a=&test (), his role is to point the memory address of the $b variable in the return $b to the same place as the $a variable's memory address
That 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 $b changed to 5.
Here is to let you understand the function of reference to return to use static variables, in fact, the function of reference return is more used in the object
references to Objects
Copy Code code as follows:

?
Class a{
var $abc = "abc";
}
$b =new A;
$c = $b;
echo $b->abc;//here output ABC
echo $c->abc;//here output ABC
$b->abc= "DEF";
echo $c->abc;//here output def
?>

The above code is the effect of the operation in PHP5
The replication of objects in PHP5 is accomplished by reference. The above $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b;
The default in PHP5 is to invoke an object by reference, but sometimes you might want to create a copy of an object and expect the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.
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.

dereference
When you 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:
Copy Code code as follows:

<?php
$a = 1;
$b =& $a;
unset ($a);
?>

Not unset $b, just $a.

Global Reference
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:
Copy Code code as follows:

<?php
$var =& $GLOBALS ["var"];
?>

This means, for example, that unset $var does not unset global variables.
$this
In the method of an object, $this is always a reference to the object that called it.

Here's a little episode.
PHP for the address of the point (like pointers) function is not implemented by the user, is implemented by the Zend Core, PHP refers to the use of "write-time copy" principle is that unless there is 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
$a = "ABC";
$b = $a;
In fact, at this point $a and $b are pointing to the same memory address and not $a and $b occupy different memory
2: If you add the following code based on the above code
$a = "EFG";
Because the $a and $b point to the memory of the data to be written again, at this time the Zend core will automatically judge automatically for $b production of a $a copy of the data, reapply a piece of memory for storage

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.