<p>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 <a href="http://php.net/date" rel="noreferrer"><code>date()</code></a> function, the manual reads:</p>

<pre><code>string date ( string $format [, int $timestamp = time() ] )
</code></pre>

<p>Where <code>$timestamp</code> is an optional parameter, and when left blank it defaults to the <a href="http://php.net/time" rel="noreferrer"><code>time()</code></a> function's return value.</p>

<p>How do you go about creating optional parameters like this when defining a custom function in PHP?</p>

[{'answer_id': 34869, 'author': 'Jeff Winkworth', 'author_id': 1306, 'author_profile': 'https://Stackoverflow.com/users/1306', 'pm_score': 9, 'selected': True, 'text': "<p>Much like the manual, use an equals (<code>=</code>) sign in your definition of the parameters:</p>\n\n<pre><code>function dosomething($var1, $var2, $var3 = 'somevalue'){\n    // Rest of function here...\n}\n</code></pre>\n"}, {'answer_id': 34877, 'author': 'mk.', 'author_id': 1797, 'author_profile': 'https://Stackoverflow.com/users/1797', 'pm_score': 4, 'selected': False, 'text': "<p>Give the optional argument a default value. </p>\n\n<pre><code>function date ($format, $timestamp='') {\n}\n</code></pre>\n"}, {'answer_id': 34999, 'author': 'gregh', 'author_id': 2687, 'author_profile': 'https://Stackoverflow.com/users/2687', 'pm_score': 5, 'selected': False, 'text': '<p>Some notes that I also found useful:</p>\n\n<ul>\n<li><p>Keep your default values on the right side.</p>\n\n<pre><code>function whatever($var1, $var2, $var3="constant", $var4="another")\n</code></pre></li>\n<li><p>The default value of the argument must be a constant expression. It can\'t be a variable or a function call.</p></li>\n</ul>\n'}, {'answer_id': 35034, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 6, 'selected': False, 'text': "<blockquote>\n  <p>The default value of the argument must be a constant expression. It can't be a variable or a function call.</p>\n</blockquote>\n\n<p>If you need this functionality however:</p>\n\n<pre><code>function foo($foo, $bar = false)\n{\n    if(!$bar)\n    {\n        $bar = $foo;\n    }\n}\n</code></pre>\n\n<p>Assuming <code>$bar</code> isn't expected to be a boolean of course.</p>\n"}, {'answer_id': 16881443, 'author': 'Lars Gyrup Brink Nielsen', 'author_id': 1071200, 'author_profile': 'https://Stackoverflow.com/users/1071200', 'pm_score': 4, 'selected': False, 'text': '<p>The date function would be defined something like this:</p>\n\n<pre><code>function date($format, $timestamp = null)\n{\n    if ($timestamp === null) {\n        $timestamp = time();\n    }\n\n    // Format the timestamp according to $format\n}\n</code></pre>\n\n<p>Usually, you would put the default value like this:</p>\n\n<pre><code>function foo($required, $optional = 42)\n{\n    // This function can be passed one or more arguments\n}\n</code></pre>\n\n<p>However, only <em>literals</em> are valid default arguments, which is why I used <code>null</code> as default argument in the first example, <strong>not</strong> <code>$timestamp = time()</code>, and combined it with a null check. Literals include arrays (<code>array()</code> or <code>[]</code>), booleans, numbers, strings, and <code>null</code>.</p>\n'}, {'answer_id': 35749448, 'author': 'Gergely Lukacsy', 'author_id': 1957951, 'author_profile': 'https://Stackoverflow.com/users/1957951', 'pm_score': 4, 'selected': False, 'text': '<p>If you don\'t know how many attributes need to be processed, you can use the variadic argument list token(<code>...</code>) introduced in PHP 5.6 (<a href="http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list" rel="noreferrer">see full documentation here</a>).</p>\n\n<p>Syntax:</p>\n\n<pre><code>function &lt;functionName&gt; ([&lt;type&gt; ]...&lt;$paramName&gt;) {}\n</code></pre>\n\n<p>For example:</p>\n\n<pre><code>function someVariadricFunc(...$arguments) {\n  foreach ($arguments as $arg) {\n    // do some stuff with $arg...\n  }\n}\n\nsomeVariadricFunc();           // an empty array going to be passed\nsomeVariadricFunc(\'apple\');    // provides a one-element array\nsomeVariadricFunc(\'apple\', \'pear\', \'orange\', \'banana\');\n</code></pre>\n\n<p>As you can see, this token basically turns all parameters to an array, which you can process in any way you like.</p>\n'}, {'answer_id': 70110910, 'author': 'Arkadii Chyzhov', 'author_id': 5554825, 'author_profile': 'https://Stackoverflow.com/users/5554825', 'pm_score': 3, 'selected': False, 'text': "<p>Starting with 7.1 there is a type hinting for nullable parameters</p>\n<pre><code>function func(?Object $object) {}\n</code></pre>\n<p>It will work for these cases:</p>\n<pre><code>func(null); //as nullable parameter\nfunc(new Object());  // as parameter of declared  type\n</code></pre>\n<p>But for optional\xa0value signature should look like.</p>\n<pre><code>function func(Object $object = null) {} // In case of objects\nfunction func(?Object $object = null) {} // or the same with nullable parameter\n\nfunction func(string $object = '') {} // In case of scalar type - string, with string value as default value\nfunction func(string $object = null) {} // In case of scalar type - string, with null as default value\nfunction func(?string $object = '') {} // or the same with nullable parameter\n\nfunction func(int $object = 0) {} // In case of scalar type - integer, with integer value as default value\nfunction func(int $object = null) {} // In case of scalar type - integer, with null as default value\nfunction func(?int $object = 0) {} // or the same with nullable parameter\n</code></pre>\n<p>than it can be invoked as\xa0\xa0</p>\n<pre><code>func(); // as optional parameter\nfunc(null); // as nullable parameter\nfunc(new Object()); // as parameter of declared type\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/34868', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2687/']