Php reference value passing instance details

Source: Internet
Author: User
In php, you only need to add an & amp; before the original object to use the reference value assignment. for details about how to use the reference value, refer to the following explanations and examples. Reference
Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system.
I. variable reference

The code is as follows:


$ A = 100;
$ B = & $;
Echo $ B; // output 100
Echo $ a; // output 100 here, indicating that the values of $ a and $ B are both one hundred.
$ B = 200;
Echo $ a; // output 200
Echo $ B; // output 200 here, which means they use the same address. Change one, and the other will also change.
?>


II. reference and pass value in the function

The code is as follows:


Function main ($ a, $ B ){
$ B = $ a + 100;
Return $ B;
}
Main (55, & $ B); // Here $ B is actually passing its memory address to the $ B parameter in the main function, the value of $ B is changed by changing the parameter $ B.
Echo $ B; // The output is 155,
?>


3. pass the reference value of an object
Object reference

The code is as follows:


Class club {
Var $ name = "real madrid ";
}
$ B = new club;
$ C = $ B;
Echo $ B-> name; // output real madrid
Echo $ c-> name; // output real madrid
$ B-> name = "ronaldo ";
Echo $ c-> name; // output ronaldo
?>


Cancel reference
When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:

The code is as follows:


$ A = 'Ronaldo'
$ B = & $;
Unset ($ );
?>


Not unset $ B, just $.

Example:
Test1.php

The code is as follows:


/**
* Reference transfer
The following content can be passed through reference:
Variable, such as foo ($)
New statement, such as foo (new foobar ())
Reference returned from the function, for example:

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

$ A = 5;
// Valid
Foo ($ );
Foo (new stdClass ());
// Illegal use
Function bar () // Note the missing &
{
$ A = 5;
Return $;
}
Foo (bar (); // fatal error caused by PHP 5.0.5
Foo ($ a = 5) // expression, not a variable
Foo (5) // cause a fatal error

?>


Test2.php

The code is as follows:


Function test (& $)
{
$ A = $ a + 100;
}
$ B = 1;
Echo $ B; // output 1
Test ($ B); // here, $ B actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function.

Echo"
";

Echo $ B; // output 101

/*****************************
*
* Note that the parameter after call_user_func_array is required &
*
*****************************/

// Do not add the & symbol before $ B in the above "test ($ B);", but in the function "call_user_func_array", to reference the parameter passing, the & symbol is required, as shown in the following code:

Function a (& $ B ){
$ B ++;
}
$ C = 0;
Call_user_func_array ('A', array (& $ c ));
Echo $ c;
// Output 1
?>


Reference return
The return value of the reference is used when you want to use the function to find the variable on which the reference should be bound. Do not use return references to increase performance. the engine is smart enough for optimization. A reference is returned only when there are reasonable technical reasons! To return a reference, use this syntax

The code is as follows:


Function & test ()
{
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo $ B;
Return $ B;
}

$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.

$ A = & test (); // This statement outputs the value of $ B as 3. here, the memory address of the $ B variable in return $ B is pointed to the same place as the memory address of the $ a variable.
$ A = 5; // The value of the $ B variable in return $ B has been changed.

$ A = test (); // This statement outputs a value of 6 for $ B.
?>


Explanation:
In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.
Php requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the php Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.

The example above is as follows:
$ A = test () is used to call a function. it only assigns the value of the function to $ a. any change made to $ a does not affect $ B in the function.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & $ B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.
An interesting example is shown on oschina:

The code is as follows:


$ 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 ($ );
?>


Will 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 $ n, that is, $ a [2] to ABE, the second cycle is changed to BEN, and the third cycle is 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.