Home >Backend Development >PHP Tutorial >How to Create and Configure Optional Parameters in PHP Functions?
Customizable Parameters in PHP Functions
Optional parameters allow functions to accept a varying number of arguments. The PHP manual illustrates this concept using brackets for dependent optional parameters, as seen in the syntax for the date() function:
string date ( string $format [, int $timestamp = time() ] )
This indicates that $timestamp is optional and defaults to the time() function's result if not specified.
Creating Optional Parameters in Custom Functions
To create optional parameters in custom PHP functions, apply an equals (=) sign to the parameter's value:
<code class="php">function dosomething($var1, $var2, $var3 = 'somevalue'){ // Rest of function here... }</code>
Here, $var3 is an optional parameter with a default value of 'somevalue'. If $var3 is not passed when the function is called, 'somevalue' will automatically be assigned to it within the function.
The above is the detailed content of How to Create and Configure Optional Parameters in PHP Functions?. For more information, please follow other related articles on the PHP Chinese website!