Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple optional argument in a function

Tags:

function

php

getAllForms($data=null)

getAllForms() and getAllForms("data")

This will work. But I want to make two optional argument in a function like this:

getAllForms($arg1=null,$arg2=null)

getAllForms() and getAllForms("data")

How can I make this possible?

like image 458
sudeep cv Avatar asked Nov 29 '22 02:11

sudeep cv


1 Answers

You can try:

function getAllForms() {
    extract(func_get_args(), EXTR_PREFIX_ALL, "data");
}

getAllForms();
getAllForms("a"); // $data_0 = a
getAllForms("a", "b"); // $data_0 = a $data_1 = b
getAllForms(null, null, "c"); // $data_0 = null $data_1 = null, $data_2 = c
like image 154
Baba Avatar answered Dec 25 '22 03:12

Baba