Reference parameter function of PHP function

Source: Internet
Author: User
Tags php programming
Introduction to functions referencing parameters

In PHP, the default is to pass parameters by value, and the parameters of the function are local variables, so even if you change the value of the parameter inside the function, it does not change the value of the outside of the function. The previous section describes general parameter functions and pseudo-type parameter functions in PHP. This section is about how to reference parameters, and when a function is a subroutine, the program that invokes the function can be called the parent program. The parent program passes the specified value or variable directly to the function's use. Because the values passed in or variables and functions are stored in different memory chunks, the function does not directly affect the parent program if it makes any changes to the imported values.

The function format description of the PHP reference parameter is as follows:

void Funname (array &arg)                     //parameter with & description appears in parameter list

The following example illustrates a php reference parameter:

<?php//Declare a function as test function ($a) {   $a = $;     function to change the value of the parameter is $b =;       The parent program declares a global variable $b and gives an initial value of ($b).       Call the test function to pass 100 to the parameter of the function echo $b;        The value of the output $b has not changed?>

In the above instance, when the test () function is called, the value of the global variable $b is passed to the function test (). Although a new value of 200 is specified for the variable $a in the test () function, it is not possible to change the value of the $b of the variable outside the function. After the call to the test () function ends, the value of the variable $b output is still 100. If you want to allow a function to modify its parameter values, you must pass the arguments by reference.

Rather than passing the pattern by value, the specified numeric value or the target variable in the parent program is not passed to the function, but instead the memory chunk of the variable is stored in the relative address import function. Therefore, when the value has any change in the function, it affects the parent program. If you want a parameter of a function to always pass by reference, in the function definition, precede the argument with the symbol & .

Modify the previous example:

<?php//Declare a function as test function tests (& $a) {   $a = $;     function to change the parameter value to 200, plus use & reference parameter $ A, external variable is modified} $b =;       The parent program declares a global variable $b and gives an initial value of ($b).       Call the test function to pass the reference of the variable $b to the function's argument $ A echo $b;        The value of the output $b is modified when the variable $ A is modified in the function?>

In the above instance, when the test () function is called, 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, a reference symbol is used & the specified variable $ A is passed by reference. A new value of 200 is specified in the function body for the variable $ A, and because the external data is modified by reference, the value of the external variable $b is also modified. After the function call finishes, you can see that the output value of the variable $b is 200.

Note: If you have used the & modifier parameter in the function's formal parameter. You must pass in a variable to this parameter when you call the function, and you cannot pass a value.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of video tutorials

3. PHP real-Combat video tutorial

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.