Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an optional parameter in PHP Function [duplicate]

Tags:

php

How can I pass an optional parameter to a PHP function. For example

function test($required, $optional){..} 

So that i should be able to call the function by passing either one parameter or both. For example:

test($required, $optional) test($required); 

Thanks.

like image 901
Peter Avatar asked Mar 26 '11 11:03

Peter


2 Answers

try this:

function test($required, $optional = NULL){..}  

then you can call

test($required, $optional) 

and with $optional null

test($required);   
like image 115
felixsigl Avatar answered Sep 19 '22 06:09

felixsigl


With a default value:

function test($required, $optional="default value"){..} 
like image 23
Skilldrick Avatar answered Sep 23 '22 06:09

Skilldrick