Home  >  Article  >  Backend Development  >  How to Create Optional Arguments in PHP?

How to Create Optional Arguments in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-20 11:48:02251browse

How to Create Optional Arguments in PHP?

Creating Optional Arguments in PHP

In PHP, you may encounter functions with optional parameters where brackets are used to group dependent optional parameters in the manual syntax. For example, in the date() function, $timestamp is optional and defaults to time().

To create similar optional parameters in custom PHP functions, simply utilize an equals (=) sign within the parameter definition.

<code class="php">function dosomething($var1, $var2, $var3 = 'somevalue'){
    // Function implementation...
}</code>

In this example, $var3 has the default value of 'somevalue'.

The above is the detailed content of How to Create Optional Arguments 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