Default parameter function of PHP function

Source: Internet
Author: User
Introduction to PHP default parameter functions

In PHP, the default invocation of a function is supported, which is to specify a default value for the parameter. The previous section describes the reference parameter function of the PHP function, which uses the default value of the parameter when the function is called without the value of the specified parameter. The default value must be an expression of a constant, not a variable, a class member, or a function call. PHP also allows the use of arrays and special types of NULL as default parameters.

The function format for PHP default parameters is described below:

Mixed Funname (string name[, string value [, int expire]])    //Parameter list appears using [] Description parameter

Arguments are declared when a function is defined, and when a function is called without specifying a parameter or specifying a parameter less, a warning of missing arguments appears.

The default parameters are described below with an instance:

<?php//Custom a function person, used to print the properties of a man ($name = "Zhang San", $age =20, $sex = "male") {   echo ' name: {$name}, Age: {$age}, Gender: {$sex} <br> "; } person ();               All use the default parameter person ("John Doe");            The first default parameter is overwritten, and the following two parameters are the default person ("John Doe", "X");          The first two default parameters are overwritten, and the following parameter is the default person ("John Doe", 22, "female");        Three default parameters are overwritten?>

After the program executes, the output is as follows:

Name: Zhang San, age: 20, Gender: Male

Name: John Doe , age: 20, Gender: Male

Name: John Doe, Age: +, sex: Male

Name: John Doe, age: 22, Gender: Female

In the above example, a function named person () with three parameters is declared, and the three parameters are appended with the initial value, the default parameter . When the function is called, the parameter will use the default value if the argument is passed or not passed. If the user calls the function simultaneous value, the passed-in value is used.

When a function is called to pass a parameter, the arguments and parameters are passed in the order corresponding to the data, and if the real argument is less than the formal parameter, the parameter on the right is not passed to the value. When using default parameters, any default parameter must be placed to the right of any non-default parameter, otherwise the function will not work as expected.

For example, the following function declaration is the incorrect use of a function's default argument. The following two parameters are not passed, and there are no default values, and a warning appears when called.

Here's an example to illustrate:

<?php//Custom a function person, used to print a human's attribute function ($name = "Zhang San", $age, $sex) {   echo "name: {$name}, Age: {$age}, Gender: {$sex } <br> "; } person ("John Doe");    The first default parameter is overwritten, and the next two parameters do not have a value, and two warning messages appear?>

Note: Only the parameter list of the function header, the default parameter is listed after all parameters that do not have a default value, the program can be executed correctly. In the above code, the function person () must pass a value parameter when the first two arguments are called, and if it does not, an error will occur, and the last parameter is an optional parameter, and the default value is used if the value is not passed.

"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.