The PHP manual employs brackets to denote optional parameters in function syntax. For instance, in the date() function, the $timestamp parameter is optional and defaults to time().
But how do we create such optional arguments when defining custom functions?
Emulating the syntax in the manual, we utilize the equals (=) sign in our parameter definitions:
function dosomething($var1, $var2, $var3 = 'somevalue'){ // Rest of function here... }
Here, $var3 is an optional argument that defaults to 'somevalue' if no value is provided. This allows for flexibility in function calls, enabling us to omit specific parameters and rely on their default values.
The above is the detailed content of How to Create Optional Arguments in Custom PHP Functions?. For more information, please follow other related articles on the PHP Chinese website!