PHP Reference Transfer Learning Note _php Tutorial

Source: Internet
Author: User
Using reference assignment in PHP only requires a & in front of the original object, $a = & $b; in fact, a reference in PHP is a variable of two different names pointing to the same value.

What is a reference?

Referencing in PHP means accessing the same variable content with a different name. This is not like the C pointer, instead, the reference is the symbol 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 filename and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be seen as hardlink in Unix file systems.

A: A reference to a variable

The code is as follows Copy Code
$a = 100;
$b = & $a;
Echo $b; Output here 100
echo $a; The output here is 100, stating $ A, and the value of $b is 100.
$b = 200;
echo $a; Output here 200
Echo $b; Output 200 here, it can be seen that they are using the same address. Change one, and the other will change.
?>

Two: The value of the reference in the function.

The code is as follows Copy Code

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

Three: The reference value of the object

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, 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:

The code is as follows Copy Code

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

Not unset $b, just $a.


example, a reference pass

test1.php

The code is as follows Copy Code

/**
* 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)//causes fatal error

?>

test2.php

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 memory address of the variable content of $b, by changing the value of $ A in the function can change the value of $b

echo "
";

echo $b;//Output 101

/*****************************
*
* Note that the parameters after Call_user_func_array are required &
*
* ****************************/

The "test" above ($b); Do not add the & symbol in front of the $b, but in the function "Call_user_func_array", to reference the arguments, 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

Reference returns are used when you want to use a function to find out which variable the reference should be bound to. Do not use return references to increase performance, and the engine is smart enough to optimize itself. Return references only if there are reasonable technical reasons! To return a reference, use this syntax

The code is as follows Copy Code
function &test ()
{
Static $b =0;//declaration of a statically variable
$b = $b +1;
Echo $b;
return $b;
}

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

$a =&test ();//This statement will output the value of the $b to 3 where the memory address of the $b variable in the return $b points to the same place as the memory address of the $ A variable
$a = 5; The value of the $b variable in the return $b has been changed

$a =test ();//This statement outputs a value of $b of 6
/**

The following explanation:
In this way $a=test (); The result is not a function reference return, which is not the same as a normal function call. The reason: It's php's rule.
PHP rules through $a=&test (); The way to get is the reference to the function is returned
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.) I haven't read this bullshit.

In the example above, the explanation is
$a =test () call the function, just assign the value of the function to $ A, and no change to $ A will affect the $b in the function.
by $a=&test (), the function is to call the memory address of the $b variable in the return $b to the same place as the memory address of the $ A variable.
That produces the equivalent effect ($a =& $b;) So changing the value of $ A also changes the value of the $b so that it executes the
$a =&test ();
$a = 5;
Later, the value of the $b becomes 5

This is to let you understand that the function reference returns only use static variables, in fact, the function of the reference return is more used in the object
*/
?>

Here's an interesting example of what you see on Oschina:

The code is as follows Copy Code

$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: The loop in the second foreach is 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, the first loop in the second foreach changes the $n, that is, $a[2] to Abe, and the second loop changes to Ben, and the third time is Ben.

http://www.bkjia.com/PHPjc/632667.html www.bkjia.com true http://www.bkjia.com/PHPjc/632667.html techarticle using reference Assignment in PHP only requires adding a $b to the original object, in fact, the reference in PHP is two variables of different names pointing to the same value. Reference is what is referenced in PHP meaning to use ...

  • 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.