PHP passing and receiving parameters in the function body _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP transmits and receives parameters in the function body. In PHP functions, parameter transfer can be divided into value transfer and reference transfer (also known as address transfer. By default, PHP transmits parameters by value. When a value transfer parameter calls a function in a PHP function, parameter transfer can be divided into value transfer and reference transfer (also known as address transfer.

By default, PHP transmits parameters by value. When a value is passed to a function, the value of a constant or variable (usually called an actual parameter) is passed to the function parameter (usually called a form parameter ). The feature of value passing is that the real participating row parameters are stored in the memory separately and are two unrelated independent variables. Therefore, when the value of the parameter is changed inside the function, the value of the real parameter is generally not changed.

The feature of reference transfer (by address transfer) is that the real participation row parameters share a piece of memory. Therefore, when the value of the parameter changes, the value of the real parameter also changes accordingly. From this perspective, it can be considered that the real parameters of the shape parameter are the same variable.

When defining the parameters passed by reference, you can add the reference symbol before the parameter &.

 // Program output: test character! Print complete

Php also supports variable-length parameter lists. When defining a function, no parameter is specified. When calling a function, you can specify the number of parameters as needed and obtain parameter information through the system functions related to the parameters. The details are as follows:

 // Program output: the function contains the following four parameter values: 10

Func_num_args function: returns the number of parameters passed to the function. The syntax is as follows: int func_num_args (void ). Description: return the number of parameters passed to the current defined function. If you call this function outside the function definition, func_get_arg () generates a warning.

Func_num_args () can be used in combination with func_get_arg () and func_get_args () to allow the user-defined function to accept the list of variable-length parameters. Func_get_arg () returns the item from the parameter list. Its syntax is int func_get_arg (int arg_num). It returns the arg_num parameter in the parameter list of the defined function. its parameter starts from 0. When you call this function outside the function definition, a warning is generated. when arg_num is greater than the number of parameters actually passed by the function, a warning is generated and FALSE is returned.

The difference between the func_get_args () function and func_get_arg () function is that the func_get_args () function returns an array. the elements of the array are equivalent to the number of parameter columns defined by the user.

When we build PHP classes, flexible use of these three functions can play an ideal effect. for example, when creating a class with PHP and MYSQL links, we can write the following code:

 0){$args=func_get_args();$this->host=$args[0];$this->user=$args[1];$this->pass=$args[2];$this->connect();}}}?>

A sample program is provided:

 n";if ($numargs >= 2) {echo "Second argument is: " . func_get_arg (1) . "
n";}$arg_list = func_get_args();for ($i = 0; $i < $numargs; $i++) {echo "Argument $i is: " . $arg_list[$i] . "
n";}}foo (1, 2, 3);?>

Forward (also known as address transfer. By default, PHP transmits parameters by value. When the value passing parameter calls the function...

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.