Summary of the use of PHP reference characters &

Source: Internet
Author: User
Tags zend

With reference to PHP (the role of & symbol in front of a variable or function, object, etc.), let's look at the following procedure.

The code is as follows Copy Code

<?php
$a = 100; Declaring variable A
$b = & $a; Declaring variable B, referencing argument a
echo "$a <br/>";
echo "$b <br/>";
$a + +; Variable a self-increase 1
echo "$a <br/>";
echo "$b <br/>";//view variable B, also increased by 1, indicating that the same storage unit is used
?>
Program Run Result:

100
100
101
101

Many people misunderstand that the reference in PHP is the same as the pointer in C, which is actually not the case, and it's a big difference. The pointer in C language is not explicitly declared except in the array pass. All others need to use * to define, and PHP for the address (like a pointer) function is not implemented by the user, is implemented by the Zend Core, PHP referenced by the "write-time copy" principle, Is that unless a write is done, a variable or object that points to the same address is not copied.

PHP defaults to pass values:

The code is as follows Copy Code

<?php
$a = 20;
$b = $a;
$a = $a + 10;
echo $a. ' and '. $b;
?>
Program Run Result:

20

If you want to change to address delivery need to add, both:

The code is as follows Copy Code

<?php
$a = 20;
$b = & $a;
$a = $a + 10;
echo $a. ' and '. $b;
?>
Program Run Result:

30

That is,,& the $a address to $b, so that the two variables now share an area of memory, meaning their values are the same.

The same syntax can be used in a function that returns a reference and is used in the new operator:

The code is as follows Copy Code
<?php
$bar =& new Fooclass ();
$foo =& Find_var ($bar);
?>

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 the variable is referenced within the call scope by the same content. The popular point is that a parameter of a function is a reference to a local variable. Here's another example:

The code is as follows Copy Code
<?php
function foo (& $val 1, $val 2) {
$val 1 = 1;
$val 2 = 1;
}
$a = 5;
$b = 10;
Foo ($a, $b);
echo $a;
Echo $b;
?>

Running this code is to pass two parameters to the function, one is to refer to the content of $a, one is the value of $b, after executing this function, found that $a content changed, and $b content is unchanged.

The third use of PHP references is to return the reference, which is a bit difficult to understand, and the reference is used when you want to use a function to find the reference to which variable it should be bound. When you return a reference, use this syntax: To say a simple point, or to refer to the return of a function. However, unlike parameter passing, you must use the & symbol in both function definitions and function references in all two places. Here's an example:

The code is as follows Copy Code
<?php
function &find_var ($param)
{
/* ... code ... * *
return $found _var;
}
$foo =& Find_var ($bar);
$foo->x = 2;
?>

This example assigns the $foo to the return reference of the function Find_var, so when assigning a value to the $foo->x, it assigns a value to Find_var's return reference, rather than a simple assignment.

Call to an address of a function

I'm not going to say any more, I'll just give you the following code:

The code is as follows Copy Code
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), the words will be wrong, the reason to think for themselves.
The function's Reference returns
First look at the code:

  code is as follows copy code

function &test ()
{
Static $b =0;//declares a static variable
$b = $b +1;
Echo $b;
return $b;
}

$a =test ();//This statement outputs $b value of 1
$a = 5;
$a =test ();//This statement outputs $b value of 2

$a =&test ();//This statement outputs $b value of 3
$a = 5;
$a =test ();//This statement outputs $b value of 6

$a=test () In this way is not actually a reference return of 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.) )
Use the example above to explain:
Calling a function $a =test () simply assigns the value of the function to $a, and $a any change does not affect the $b in the function. The function is called by $a=&test (), and his function is to point the memory address of the $b variable in the return $b with the memory address of the $a variable to the same place 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

The code is as follows Copy Code
$a =&test ();
$a = 5;

Later, the value of the $b changed to 5.
Here is to let you understand the function of reference return to use static variables, in fact, the function of reference return is more used in the object.
References to Objects

The code is as follows Copy Code
?
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 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 the reference: if the program is larger, the reference to the same object more variables, and want to use the object after the manual removal of it, personal advice to "&" mode, and then use the $var=null way to clear. 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.
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:

The code is as follows Copy Code
<?php
$a = 1;
$b =& $a;
unset ($a);
?>

Not unset $b, just $a.
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:

The code is as follows Copy Code
<?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.
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, if you have the following code:
$a = "ABC";
$b = $a;
In fact at this time $a and $b are pointing to the same memory address, rather than $a and $b occupy different memory.
If you add the following code based on the above code:
$a = "EFG";
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.

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.