Reference parameter functions of php functions

Source: Internet
Author: User
In php, parameters are passed by value by default, and the function parameters are also local variables. Therefore, even if the parameter value is changed inside the function, it does not change the external value of the function. Introduction to functions that reference parameters

In php, parameters are passed by value by default, and the function parameters are also local variables. Therefore, even if the parameter value is changed inside the function, it does not change the external value of the function. The previous section describes common parameter functions and pseudo-type parameter functions in php. This section describes how to reference parameters. when a function is a subroutine, the program that calls the function can be called a parent program. The parent program directly transmits the specified value or variable to the function. Because the passed values or values in the variables and functions are stored in different memory blocks, if the function makes any changes to the imported values, does not directly affect the parent program.

The format of the function for referencing parameters in php is described as follows:

Void funName (array & arg) // in the parameter list, parameters described with "use" are displayed.

The following example describes the php reference parameters:

 

In the above example, when calling the test () function, pass the value of the global variable $ B to the function test (). Although a new value of 200 is specified for variable $ a in the test () function, the value of variable $ B outside the function cannot be changed. After the test () function is called, the output value of variable $ B is still 100. If you want to allow a function to modify its parameter values, you must pass parameters through reference.

Compared to the value-based transmission mode, the specified value or target variable in the parent program is not transferred to the function. Instead, the memory storage block of the value or variable is imported to the function. Therefore, when the value changes in the function, it may affect the parent program. If you want a function parameter to always pass through the reference, add a symbol before the parameter in the function definition.&You can.

Modify the previous instance:

 

In the above example, when calling the test () function, the value of the global variable $ B is not passed to the function test (). We can see that in the definition of the test () function, the reference symbol & specified variable $ a is used as the reference transfer mode. In the function body, a new value of 200 is specified for the variable $ a. because the external data is modified by reference, the value of the external variable $ B is also modified. After the function is called, the output value of variable $ B is 200.

Note: If this parameter is used in a function&Modify parameters. When calling this function, you must input a variable to this parameter, but not a value.

[Recommended tutorials]

1. php1.cn Dugu jiujian (4)-php video tutorial

2. php programming from getting started to mastering a full set of video tutorials

3. php video tutorial

The above is the reference parameter function details of php functions. For more information, see other related articles in the first PHP community!

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.