Home >Backend Development >PHP Tutorial >PHP function reference parameter function
Introduction to functions that reference parameters
In PHP, parameters are passed by value by default, and the parameters of the function are also local variables, so even in the function Changing the value of a parameter internally does not change the value outside the function. The previous section introduced the regular parameter functions and pseudo-type parameter functions in php. This section will talk about how to reference parameters. When the function is a subprogram, the program that calls the function can be called the parent program. The parent program directly passes the specified value or variable to the function for use. Since the passed value or variable and the value in the function are stored in different memory blocks, if the function makes any changes to the imported value, it will not directly affect the parent program.
The function format description of php reference parameters is as follows:
void funName(array &arg) // 在参数列表中出现使用 &描述的参数
The following is an example of php reference parameters:
<?php //声明一个函数作为测试 function test($a){ $a = 200; // 函数中改变参数值为200 } $b = 100; // 父程序中声明一个全局变量$b并给一个初始值100 test($b); // 调用test函数 将100传给函数的参数 echo $b; // 输出100 $b的值没变化 ?>
In In the above example, when calling the test() function, the value of the global variable $b is passed to the function test(). Although the variable $a is assigned a new value of 200 in the test() function, the value of the variable $b outside the function cannot be changed. After calling the test() function, the value output by the variable $b is still 100. If you want to allow a function to modify its argument values, you must pass the argument by reference.
Compared to the pass-by-value mode, the specified value or target variable in the parent program is not passed to the function, but the relative address of the memory storage block of the value or variable is imported into the function. Therefore, when the value is changed in any way in the function, it will also affect the parent program. If you want a parameter of a function to always be passed by reference, prepend the symbol & in the function definition.
Modify the previous example:
<?php //声明一个函数作为测试 function test(&$a){ $a = 200; // 函数中改变参数值为200,加使用 & 引用参数$a,外部变量被修改 } $b = 100; // 父程序中声明一个全局变量$b并给一个初始值100 test($b); // 调用test函数 将变量$b的引用传给函数的参数$a echo $b; // 输出200 $b的值在函数中修改变量$a时被修改 ?>
In the above example, when calling the test() function, the value of the global variable $b is not passed to the function. test(). As you can see, in the definition of the test() function, the reference symbol & is used to specify that the variable $a is passed by reference. In the function body, the variable $a is assigned a new value of 200. Since the external data will be modified by reference, the value of the external variable $b is also modified. After the function call ends, you can see that the output value of variable $b is 200.
Note: If you use & to modify the parameters in the formal parameters of the function. Then when calling the function, you must pass in a variable to this parameter, but not a value.
【Recommended related tutorials】
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2. php programming from entry to master a full set of video tutorials
3. php practical video tutorial
The above is the detailed content of PHP function reference parameter function. For more information, please follow other related articles on the PHP Chinese website!