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

How to Create Optional Parameters in PHP

Mary-Kate Olsen
Release: 2024-10-20 12:52:02
Original
983 people have browsed it

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() ] )
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template