PHP Reference Pass Value Learning notes

Source: Internet
Author: User
Tags foreach

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.

One: A reference to a variable

The code is as follows Copy Code
<?php
$a = 100;
$b = & $a;
Echo $b; Output 100 here
echo $a; Here is the output of 100, which shows that $a, and $b values are all 100.
$b = 200;
echo $a; Output 200 Here
Echo $b; Output 200 here, which shows that they are using the same address. Change one, and the other will change accordingly.
?>

Two: The reference in the function is passed the value.

The code is as follows Copy Code

<?php
function main ($a, $b) {
$b = $a +100;
return $b;
}
Main (55,& $b); The $b is actually passing its memory address to the $b parameter in function main, changing the value of the outer $b by changing the parameter $b.
Echo $b; This will output 155,
?>

Three: object's reference Pass value

References to Objects

The code is as follows Copy Code
?
Class club{
var $name = "Real Madrid";
}
$b =new Club;
$c = $b;
echo $b->name;//here output Real Madrid
echo $c->name;//here output Real Madrid
$b->name= "Ronaldo";
echo $c->name;//here output Ronaldo
?>

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:

The code is as follows Copy Code

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

Not unset $b, just $a.


example, reference passing

test1.php

The code is as follows Copy Code

<?php
/**
* Reference Delivery
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:

*/
function foo (& $var)
{
$var + +;
}

$a = 5;
Legal
Foo ($a);
Foo (new StdClass ());
illegal use
function bar ()//Note the missing &
{
$a = 5;
return $a;
}
Foo (bar ()); Fatal error since PHP 5.0.5
Foo ($a = 5)//expression, not variable
Foo (5)//lead to fatal error

?>

test2.php

  code is as follows copy 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 $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

/*****************************
*
* Here you need to note that the Call_user_func_array parameter is required &
*
* ****************************/

"Test ($b) above; "In the $b, do not precede the & symbol, but in the function" Call_user_func_array ", to refer to the argument, you need the & symbol, as shown in the following code:

Function A (& $b) {
$b + +;
}
$c = 0;
Call_user_func_array (' A ', Array (& $c));
Echo $c;
Output 1
?>

Reference returns

The

Reference returns 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

  code is as follows copy code
<?php
function &test ()
{
Static $b =0;//declares a static 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 is 2
 
    $a =&test ();//This statement outputs $b value of 3& nbsp Here the memory address of the $b variable in the return $b is pointed to the same place as the $a variable's memory address
    $a =5; //has changed the value of $b variable in the return $b
 
    $a =test ()//This statement will output $b value is 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
*/
?>

An interesting example of this is seen on the Oschina:

The code is as follows Copy Code

<?php
$a = Array (' Abe ', ' Ben ', ' cam ');
foreach ($a as $k =>& $n)
$n = Strtoupper ($n);
foreach ($a as $k => $n)//Notice NO reference here!
echo "$nn";
Print_r ($a);
?>

would result in:

ABE
Ben
Ben
Array
(
[0] => ABE
[1] => BEN
[2] => BEN
)

Explanation: Loop through the second foreach as follows:

Array
(
[0] => ABE
[1] => BEN
[2] => ABE
)
Array
(
[0] => ABE
[1] => BEN
[2] => BEN
)
Array
(
[0] => ABE
[1] => BEN
[2] => BEN
)

Because there is no unset ($n), it always points to the last element of the array, and the first loop of the second foreach $n, that is $a[2, to Abe, the second cycle to Ben, and the third time Ben.

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.