PHP function reference parameter function

伊谢尔伦
Release: 2023-03-07 18:34:01
Original
3585 people have browsed it

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)                     // 在参数列表中出现使用 &描述的参数
Copy after login

The following is an example of php reference parameters:

Copy after login

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:

Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!