Home > Backend Development > PHP Tutorial > Optional parameters in PHP

Optional parameters in PHP

WBOY
Release: 2024-03-01 09:36:02
forward
973 people have browsed it

php editor Apple will introduce to you the optional parameters in PHP today. In PHP functions, we can define some parameters as optional parameters, so that all parameters do not need to be passed in when calling the function, thereby achieving more flexible function calling. Through the introduction of this article, you will understand how to define and use optional parameters in PHP functions, as well as precautions and common use cases. Let’s take a closer look at the optional parameters in PHP functions!


Use "NULL" as optional parameter

We will create a function and pass it a default parameter with its value set to "null". If we call the function without resetting the default parameter value, "null" will be used instead.

<?php
function fruits($bestfruit = "NULL")
{
return "I love enjoying $bestfruit" .&#39;<br>&#39;;
}
echo fruits();
echo fruits(&#39;manGo&#39;);
?>
Copy after login

Output:

I love enjoying NULL
I love enjoying mango
Copy after login

Using specific values ​​as optional parameters

We will create a function and pass it a default parameter and set its value to String. If we call the function without resetting the default parameter value, then the specified value will be used instead of it.

<?php
function fruits($bestfruit = "Apple")
{
return "I love enjoying $bestfruit" .&#39;<br>&#39;;
}
echo fruits();
echo fruits(&#39;mango&#39;);
?>
Copy after login

Output:

I love enjoying Apple
I love enjoying mango
Copy after login

Use empty string as optional parameter

Create a function and pass a default parameter and set its value to the empty string.

<?php
function fruits($bestfruit = "")
{
return "I love enjoying $bestfruit" .&#39;<br>&#39;;
}
echo fruits();
echo fruits(&#39;PineApples&#39;);
?>
Copy after login

Output:

I love enjoying
I love enjoying PineApples
Copy after login

Define optional parameters using the Splat operator (...

)

Here we are not passing any default value. Instead, we will pass the splat operator (...), which will default to an empty array when no arguments are passed to the function.

<?php
function fruits(...$bestfruit)
{
var_dump($bestfruit).&#39;<br>&#39;;
}
echo fruits();
echo fruits(&#39;PineApples&#39;,&#39;test&#39;);
?>
Copy after login

Output:

 array(0) { } array(2) { [0]=> string(10) "PineApples" [1]=> string(4) "test" }
Copy after login

Use the func_get_args method to set optional parameters in PHP

Same as using the splat operator (...), we create a function without passing any default value. If we call the function without specifying a value, 0 will be the default value.

<?php
function summation() {
$numbers = func_get_args();
return array_sum($numbers);
}
 echo summation().&#39;<br>&#39;;
 echo summation(1,2,3,4,5,6);
?>
Copy after login

Output:

0
21
Copy after login

The above is the detailed content of Optional parameters in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template