Creating Optional Parameters in PHP
In PHP, the function syntax for optional parameters is denoted by square brackets around the parameter set. For instance, the date() function expects the following format:
string date ( string $format [, int $timestamp = time() ] )
Here, $timestamp is optional and defaults to the value returned by time().
Defining Optional Parameters in Custom Functions
To create optional parameters in your own custom functions, simply utilize the equals sign (=) within the parameter definition. For example:
<code class="php">function doSomething($var1, $var2, $var3 = 'somevalue'){ // Rest of function here... }</code>
In this example, $var3 is optional and will default to 'somevalue' if not specified.
The above is the detailed content of How to Create Optional Parameters in PHP. For more information, please follow other related articles on the PHP Chinese website!