About references (&) in PHP

Source: Internet
Author: User

About references (&) in PHP

PHP references (add & symbols before variables or functions, objects, etc.)

The reference in PHP means: Different variable names access the same variable content.

There is a difference between the pointers in the C language. The pointer inside the C language stores the address where the contents of the variable are stored in memory.

A reference to a variable

PHP references allow you to use two variables to point to the same content

<?php

$a = "ABC";

$b =& $a;

echo $a;//output here: ABC

echo $b;//output here: ABC

$b = "EFG";

echo $a;//The value of $ A here becomes EFG so the output EFG

echo $b;//Output EFG here

?>

Invocation of a function's address

Address call I'm not going to say much. Directly below the code

<?php

Function test (& $a)

{

$a = $a +100;

}

$b = 1;

Echo $b; Output 1

Test ($b);

Here $b passed to the function is actually the memory address of the variable content of $b, by changing the value of $ A in the function can change the value of $b

echo "<br>";

Echo $b; Output 101

?>

It is important to note that here Test (1), the words will be wrong, reason to think.

A reference to the function returns

Look at the code first

<?php

function &test ()

{

static $b = 0; Declare a static variable

$b = $b +1;

Echo $b;

return $b;

}

$a =test (); This statement will output a value of 1 for the $b

$a = 5;

$a =test (); This statement will output a value of 2 for the $b

$a =&test (); This statement will output a value of 3 for the $b

$a = 5;

$a =test (); This statement will output a value of 6 for the $b

?>

The following explanation:

In this way $a=test (); The result is not a function reference return, which is no different from the normal function call, as for the reason:

This is the PHP rule, PHP rules through $a=&test (); The way to get is to return the reference to the function. As for what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) )

This is explained by the example above:

$a =test () call the function, just assign the value of the function to $ A, and $ A does nothing to affect the $b in the function and call the function by $a=&test (), his function is to return the memory address of the $b variable in the $b with the The memory address of a variable points to the same place, which produces the equivalent effect ($a =& $b;)  So changing the value of $ A also changes the value of the $b so that it executes $a =&test ();  $a = 5; Later, the value of the $b becomes 5.

This is to let everyone understand that the function reference returns only use static variables, in fact, the function of the reference return is used in the object.

References to Objects

<?php

Class a{

var $abc = "abc";

}

$b =new A;

$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 result of running in PHP5, and the copy of the object in PHP5 is implemented 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 the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.

The role of references

If the program is larger, referencing the same object is more variable, and you want to use the object after the manual removal of it, the personal suggestion "&" method, and then $var=null to clear. At other times, it's the default way to use PHP5. In addition, in the PHP5 for large arrays of delivery, it is recommended to use "&" mode, after all, saving memory space to use.

Dereference

When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

<?php

$a = 1;

$b =& $a;

unset ($a);

?>

Not unset $b, just $a.

Global references

When you declare a variable with the global $var, you actually establish a reference to the global variable. In other words, it is the same as doing this:

<?php

$var =& $GLOBALS ["var"];

?>

This means, for example, that the 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.

Small episode

PHP in the direction of the address (similar to the pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation, the same address to the variable or object is not copied.

The popular Speaking

1: If you have the following code

$a = "ABC";

$b = $a;

In fact, $a and $b both point 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";

Since $ A and $b point to the memory of the data to be re-written once, at this time the Zend core will automatically decide to automatically produce a $ A copy of the data for $b, re-request a piece of memory for storage.

About references (&) in PHP

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.