How do you create optional arguments in php?

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?

7 Answers

Much like the manual, use an equals (=) sign in your definition of the parameters:

function dosomething($var1, $var2, $var3 = 'somevalue'){ // Rest of function here... } 
2

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.

2

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.

Give the optional argument a default value.

function date ($format, $timestamp='') { } 

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.

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.

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 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like