Home  >  Article  >  Backend Development  >  What is the difference between passing value and passing address in php

What is the difference between passing value and passing address in php

青灯夜游
青灯夜游Original
2021-11-29 18:14:173108browse

Difference: Passing value means copying the content of the original variable and saving it in a new memory space. The two variables are independent of each other. Modifying one variable will not affect the other variable. Pass-by-reference (pass-by-reference) is equivalent to giving the current variable an alias. In fact, the two variables refer to the same value, and modifying one variable will affect the other variable.

What is the difference between passing value and passing address in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Value transfer

Value passing is the default value passing method for functions in PHP, also known as "copy passing by value". As the name suggests, the value passing method will copy the value of the actual parameter and then pass it to the formal parameter of the function, so operating the value of the parameter in the function will not affect the actual parameters outside the function. Therefore, if you do not want the function to modify the value of the actual parameter, you can pass it by value.

[Example] The following defines a simple function. The function has two parameters, and the values ​​of the parameters are exchanged in the function.

<?php
    function swap($a, $b){
        echo &#39;函数内,交换前 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
        $temp = $a;
        $a = $b;
        $b = $temp;
        echo &#39;函数内,交换后 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
    }
    $x = 5;
    $y = 7;
    echo &#39;函数外,交换前 $x = &#39;.$x.&#39;, $y = &#39;.$y.&#39;<br>&#39;;
    swap($x, $y);
    echo &#39;函数外,交换后 $x = &#39;.$x.&#39;, $y = &#39;.$y;
?>

The running results are as follows:

函数外,交换前 $x = 5, $y = 7
函数内,交换前 $a = 5, $b = 7
函数内,交换后 $a = 7, $b = 5
函数外,交换后 $x = 5, $y = 7

It can be seen from the running results that the values ​​​​are indeed exchanged within the function, but outside the function, the values ​​​​do not change. So we can say that passing a function by value is just passing a copy of the variable. So if you want the function to be able to operate on external parameters of the function, you need to use reference passing.

Address passing (reference passing)

Parameter passing by reference is to copy the memory address of the actual parameter and then pass it to the formal parameter, actual parameter and The formal parameters all point to the same memory address, so the function's operation on the formal parameters will affect the actual parameters outside the function.

Passing by reference is to pass the memory address of the actual parameter to the formal parameter of the function. Therefore, the actual parameters and formal parameters point to the same memory address. At this time, all operations inside the function will affect the values ​​of the actual parameters outside the function. The method of passing by reference is to add an & symbol on the basis of value passing, as shown below:

function name (&参数1, &参数2, ..., &参数3) {
    ...    
}

[Example] Slightly adjust the code of the above example and use the method of passing by reference to pass to the swap function. Parameters and code are as follows:

<?php
    function swap(&$a, &$b){
        echo &#39;函数内,交换前 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
        $temp = $a;
        $a = $b;
        $b = $temp;
        echo &#39;函数内,交换后 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
    }
    $x = 5;
    $y = 7;
    echo &#39;函数外,交换前 $x = &#39;.$x.&#39;, $y = &#39;.$y.&#39;<br>&#39;;
    swap($x, $y);
    echo &#39;函数外,交换后 $x = &#39;.$x.&#39;, $y = &#39;.$y;
?>

The running results are as follows:

函数外,交换前 $x = 5, $y = 7
函数内,交换前 $a = 5, $b = 7
函数内,交换后 $a = 7, $b = 5
函数外,交换后 $x = 7, $y = 5

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the difference between passing value and passing address in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn