PHP parameters reference


In the variable function, we learned the reference to the variable, let's review the knowledge:

<?php
$a = 10;
$b = &$a;
$a = 100;
Echo $a.'---------'.$b;
?>
In the variable section of the appeal knowledge point, the variable reference is described, meaning that the variables $a and $b point to the same storage location to store the value.

The parameter reference of the function is also the meaning of pointing the formal parameters and arguments to the same position. If the formal parameter changes within the body of the function, the value of the actual parameter also changes. Let's take a look at the experiment:

<?php
$foo = 100;
//Note: the ampersand is added before $n
Function demo(&$n){
       $n = 10;
       Return $n + $n;
}
Echo demo($foo).'';
//You will get the value of $foo to become 10
Echo $foo;
?>
In the above example, we found that the argument is $foo. When the demo is called, let $foo and $n point to the same storage area when the value of $n changes. Then the value of $foo also changes.


Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :