Home  >  Article  >  Backend Development  >  How to Create Optional Parameters in PHP

How to Create Optional Parameters in PHP

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-20 12:52:02902browse

How to Create Optional Parameters in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn