How do you create optional arguments in php?
PhpPhp Problem Overview
In the PHP manual, to show the syntax for functions with optional parameters, they use brackets around each set of dependent optional parameter. For example, for the date()
function, the manual reads:
string date ( string $format [, int $timestamp = time() ] )
Where $timestamp
is an optional parameter, and when left blank it defaults to the time()
function's return value.
How do you go about creating optional parameters like this when defining a custom function in PHP?
Php Solutions
Solution 1 - Php
Much like the manual, use an equals (=
) sign in your definition of the parameters:
function dosomething($var1, $var2, $var3 = 'somevalue'){
// Rest of function here...
}
Solution 2 - Php
> The default value of the argument must be a constant expression. It can't be a variable or a function call.
If you need this functionality however:
function foo($foo, $bar = false)
{
if(!$bar)
{
$bar = $foo;
}
}
Assuming $bar
isn't expected to be a boolean of course.
Solution 3 - Php
Some notes that I also found useful:
-
Keep your default values on the right side.
function whatever($var1, $var2, $var3="constant", $var4="another")
-
The default value of the argument must be a constant expression. It can't be a variable or a function call.
Solution 4 - Php
Give the optional argument a default value.
function date ($format, $timestamp='') {
}
Solution 5 - Php
The date function would be defined something like this:
function date($format, $timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
// Format the timestamp according to $format
}
Usually, you would put the default value like this:
function foo($required, $optional = 42)
{
// This function can be passed one or more arguments
}
However, only literals are valid default arguments, which is why I used null
as default argument in the first example, not $timestamp = time()
, and combined it with a null check. Literals include arrays (array()
or []
), booleans, numbers, strings, and null
.
Solution 6 - Php
If you don't know how many attributes need to be processed, you can use the variadic argument list token(...
) introduced in PHP 5.6 (see full documentation here).
Syntax:
function <functionName> ([<type> ]...<$paramName>) {}
For example:
function someVariadricFunc(...$arguments) {
foreach ($arguments as $arg) {
// do some stuff with $arg...
}
}
someVariadricFunc(); // an empty array going to be passed
someVariadricFunc('apple'); // provides a one-element array
someVariadricFunc('apple', 'pear', 'orange', 'banana');
As you can see, this token basically turns all parameters to an array, which you can process in any way you like.
Solution 7 - Php
Starting with 7.1 there is a type hinting for nullable parameters
function func(?Object $object) {}
It will work for these cases:
func(null); //as nullable parameter
func(new Object()); // as parameter of declared type
But for optional value signature should look like.
function func(Object $object = null) {} // In case of objects
function func(?Object $object = null) {} // or the same with nullable parameter
function func(string $object = '') {} // In case of scalar type - string, with string value as default value
function func(string $object = null) {} // In case of scalar type - string, with null as default value
function func(?string $object = '') {} // or the same with nullable parameter
function func(int $object = 0) {} // In case of scalar type - integer, with integer value as default value
function func(int $object = null) {} // In case of scalar type - integer, with null as default value
function func(?int $object = 0) {} // or the same with nullable parameter
than it can be invoked as
func(); // as optional parameter
func(null); // as nullable parameter
func(new Object()); // as parameter of declared type