Three methods for passing php UDF parameters between functions

Source: Internet
Author: User
When calling a function, you need to pass parameters to the function. the passed parameters become real parameters, and the parameters for function definition are form parameters. Parameters between functions can be passed by value, by reference, or by default. When calling a function, you need to pass parameters to the function. the passed parameters become real parameters, and the parameters for function definition are form parameters. In the previous section, we have provided examples of php user-defined function parameters. This section focuses on three methods for passing parameters between functions: Pass by value, Pass by referenceAnd Default parameters.

1. pass by value

Assign the value of the real parameter to the corresponding parameter. The operation in the function is performed on the parameter. The operation result does not affect the real parameter. that is, after the function is returned, the value of the real parameter will not change. During the pass-by-value process, parameters in the form of the called function are processed as local variables of the called function, that is, the memory space is opened in the stack to store the values of the real parameters put in by the main function, thus becoming a copy of the real parameters. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function.

First, define a function example (). The function is to perform some operations on the input parameter values and then output them. then, define a variable $ m outside the function, that is, a real parameter; finally, call the function example ($ m ). The $ m and $ m parameters are output in the function body and in vitro respectively.

The instance code is shown as follows::

 "; // Output parameter value} $ m = 1; example ($ m); // pass the value of the real parameter $ m to the form parameter $ m echo" outside the function: m = ". $ m; // The value of the real parameter has not changed. $ m = 1?>

The function running result is as follows:

2. Transfer by reference

Passing by reference means passing the memory address of the real parameter to the form parameter. In this case, all operations on the form parameter inside the function will affect the value of the real parameter. After the function is returned, the value of the real parameter changes. The reference transfer method is to add"&"Symbol. During the pass-by-reference process, the form parameter of the called function opens up the memory space in the stack as a local variable, however, the address of the real variable put in by the main function is stored. Any operation of the called function on the form parameter is processed as indirect addressing. that is, the address stored in the stack is used to access the real variable in the main function. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function.

The sample code is as follows:

 "; // Output parameter value} $ m = 1; example ($ m); // pass the address of the real parameter $ m to the form parameter $ mecho" outside the function: m = ". $ m; // The value of the real parameter has changed. $ m = 11?>

The function running result is as follows:

3. default parameters (optional)

There is also a way to set parameters, that is, optional parameters. You can specify a parameter as an optional parameter, put it at the end of the parameter list, and specify that its default value is null.

An example of an application uses optional parameters to implement a simple price calculation function. Set $ tax as an optional parameter for the user-defined function values. its default value is null. This function is called for the first time, and the $ tax parameter is assigned a value to the output price. if this function is called for the second time, the parameter is not copied and the output price.

The code is as follows:

 "; // Output price} value (); // The optional parameter value is 5 value (10); // no value is assigned to the optional parameter?>

Output result:

Price: 50

Price: 0

Note: When default parameters are used, the default parameters must be placed on the right of non-default parameters; otherwise, function errors may occur.

Note: Starting from PHP5, the default value can also be passed through reference. The following chapter describes what is the return value of a UDF.

[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 a detailed explanation of the three methods for passing parameters of php user-defined functions between 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.