In PHP, you can call a function by default, that is, specify a default value for the parameter. If no parameter value is specified when a function is called, the default value of the parameter is used in the function. The default value must be a constant expression and cannot be a variable, a class member, or a function call.
Introduction to default php parameter functions
In PHP, you can call a function by default, that is, specify a default value for the parameter. The previous section describes the reference parameter functions of php functions. If no parameter value is specified when a function is called, the default value of the parameter is used in the function. The default value must be a constant expression and cannot be a variable, a class member, or a function call. PHP also allows the use of arrays and special typesNULLAs the default parameter.
Default php parametersThe function format is described as follows:
Mixed funName (string name [, string value [, int expire]) // use the [] description parameter in the parameter list
When defining a function, the parameter is declared. If no parameter is specified or no parameter is specified when the function is called, the parameter is missing.
The following describes the default parameters through an instance:
";}Person (); // all use the default parameter person (" Li Si "); // the first default parameter is overwritten, and the next two parameters are default persons (" Li Si ", 22); // The first two default parameters are overwritten, and the next parameter is the default person ("Li Si", 22, ""); // are all three default parameters overwritten?>
After the program is executed, the output result is as follows:
Name: zhang san, Age: 20, Gender: Male
Name: li si, Age: 20, Gender: Male
Name: Li Si, Age: 22,Gender: Male
Name: Li Si, Age: 22, Gender: Female
In the above example, a function named "person ()" with three parameters is declared. The three parameters are attached with the initial values by default, that isDefault parameters. When you call this function, if you do not transmit or disable parameters, the parameter uses the default value. If you pass a value when calling a function, the passed value is used.
When you call a function to pass parameters, the real parameters and the form parameters are transmitted in order. if the real parameters are less than the form parameters, the right side of the form parameters will not be passed. When using the default parameter, any default parameter must be placed in any non-default parameterRight sideOtherwise, the function may not work as expected.
For example, the following function declaration is an incorrect usage of the default function parameters. The following two parameters are not passed or have no default values. a warning is prompted during the call.
The following is an example:
";} Person (" Li Si "); // the first default parameter is overwritten. if the last two parameters are not passed, two warning messages are displayed.
Note: You only need to list the parameters in the function header. the default parameters are listed after all parameters without the default values, and the program can be correctly executed. In the code above, when the person () function is called, the first two parameters must be passed the value parameter. If this parameter is not passed, an error occurs. the last parameter is an optional parameter, if no value is input, the default value is used.
[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 description of the default parameter functions of php functions. For more information, see other related articles in the first PHP community!