PHP function Transfer value and address (reference) detailed _php tutorial

Source: Internet
Author: User
In PHP, our function is relatively simple, but may be some friends of innocence function address or reference do not understand, the following small part to introduce in PHP function value and address (citation) introduced, I hope to help you.

Use of references in PHP:

1. Assigning a reference to a variable: $a = & $b

2. Reference parameter passing when a function call

1) Early PHP is the variable that passes the reference type through the & symbol when it is invoked, for example: Func (& $arg);

2) Later, the reference type parameter of the function is defined as requiring a function declaration, rather than: function func (& $arg);

Note: When a reference-type parameter is defined in the reference declaration, the run-time reference parameter pass is discarded and needs to be added allow_call_time_pass_reference in the php.ini to open.

3. The function returns a reference type, which is required to declare the function, add the & symbol before the function name, and, when called, use the reference assignment method, such as:

The code is as follows Copy Code


function &func () {
return $a;
}
$a = func (); This method of invocation does not get a reference value
$a =& func (); This invocation is the reference value.

In short, let's return a value of a reference type to a function, a more practical example:

The code is as follows Copy Code

$a = 1;
Function &func (& $a) {
return $a;
}
$b = func ($a);
$c =& func ($a);
$b = 2;
echo "A: $a, B: $b, C: $c.
/n ";
Output a:1, B:2, C:1.
Visible changes to $b do not affect $ A
$c = 3;
echo "A: $a, B: $b, C: $c.
/n ";
Output A:3, B:2, C:3.
Visible changes to $c will affect $ A


A few details of the PHP function's passing value

The code is as follows Copy Code
First, the value of the basic data type
/* **************************************************** */
function TestVar ($k) {
$k = 40;
}
$c = 30;
A basic data type (integer, Boolean, character ...) is passed to a function parameter, which is actually a value;
TestVar ($c);
echo $c;//result is: 30


Function Testvar2 (& $k) {
$k = 40;
}
$e = 30;
A basic data type (integer, Boolean, character ...) is passed to a function parameter, in fact the y is the address;
TESTVAR2 ($e);
echo $e;//result is: 40

/* **************************************************** */
Second, the array (by default, a copy of the data), if you want to pass the address & $arr.
$arr 1 = array ( -1,5,0);
function Testarr ($arr) {
for ($i =0; $i
for ($j = $i +1; $j <>
if ($arr [$i]> $arr [$j]) {
$temp = $arr [$i];
$arr [$i] = $arr [$j];
$arr [$j] = $temp;
}
}

}
Print_r ($arr); Result: Array ([0] =-1 [1] = 0 [2] = 5)
}
Testarr ($arr 1);
Print_r ($arr 1); Result: Array ([0] =-1 [1] = 5 [2] = 0)

Function testArr2 (& $arr) {
for ($i =0; $i
for ($j = $i +1; $j <>
if ($arr [$i]> $arr [$j]) {
$temp = $arr [$i];
$arr [$i] = $arr [$j];
$arr [$j] = $temp;
}
}

}
}
Testarr ($arr 1);
Print_r ($arr 1); Result: Array ([0] =-1 [1] = 0 [2] = 5)




/* **************************************************** */
Third, the object data type transmission value
Class person{
Public $name;
Public $age;
}

$a = new person ();
$a->name = ' Xiao Ming ';
$a->age = ' 20 ';
Variable A in the memory is the address of the object, assign a to the variable B, is actually assigned an address.
$b = $a;
$b->age = 30;
echo $a->age. $b->age;//results are: 30 30
An object is passed to a function parameter, which is actually the address of the object;
function test ($k) {
$k->age = 40;
}
Call
Test ($b);
echo $a->age. $b->age;//results are: 40 40

What is the difference between the value of a PHP function and the address of a transfer? I like to get answers to these questions through a program demo. Let's take a look at a demo record!

The code is as follows Copy Code

$i = 100;

function func ($n) {

$n = $n +100;

return $n;

}
Echo ' 1) The value of the variable $i before the function is passed: '. $i. '
';
Echo ' 2) The return value of the function after the value: '. Func ($i). '
';
Echo ' 3) The value of the variable $i after the function is passed: '. $i. '
';
Echo ' 4) The value of the variable $i before the function is transmitted: '. $i. '
';
Echo ' 5) The return value of the function after the address: '. Func (& $i). '
';
Echo ' 6) The value of the variable $i after the function is transmitted: '. $i. '
';
echo func ($i). '
';
?>

Program output:

1) value of the variable $i before the function is passed: 100
2) return value of function after passing value: 200
3) value of variable $i after function pass value: 100
4) The value of the variable $i before the function is transmitted: 100
5) return value of function after address: 200
6) The value of the variable $i after the function is transmitted: 200

Explain:

1) Directly output a variable of assignment $i=100
2) The value of the Func function returns the result of an arithmetic addition $=100+100
3) The function of the Func function is limited to the inside of the function, and it will not affect the outside variable $i.
4) Ibid.
5) The Func function parameter has more than one "&" character, indicating the address, and 2), the return value is the arithmetic run result of the function.

6) func (& $i), the variable pointer points to the position of $i, can be understood as the operation of the variable $i, $i = $i +100; the variable $i is already the equivalent of the re-assigned value

What happens if I remove the 18th line of comments from the above code?

Summary: In order to change the value of the function parameter while executing the function, the value of the transfer is not expected to change.

http://www.bkjia.com/PHPjc/632691.html www.bkjia.com true http://www.bkjia.com/PHPjc/632691.html techarticle in PHP, our function is relatively simple, but may be some friends of innocence function address or reference do not understand, the following small part to give you introduce in PHP function transfer value and address (citation ...

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