Ok, I’m looking into using create_function for what I need to do, and I don’t see a way to define default parameter values with it. Is this possible? If so, what would be the best approach for inputting the params into the create_function function in php? Perhaps using addslashes?
Well, for example, I have a function like so:
function testing($param1 = 'blah', $param2 = array())
{
if($param1 == 'blah')
return $param1;
else
{
$notblah = '';
if (count($param2) >= 1)
{
foreach($param2 as $param)
$notblah .= $param;
return $notblah;
}
else
return 'empty';
}
}
Ok, so how would I use create_function to do the same thing, adding the parameters and their default values?
The thing is, the parameters are coming from a TEXT file, as well as the function itself.
So, wondering on the best approach for this using create_function and how exactly the string should be parsed.
Thanks 🙂
Considering a function created with
create_functionthis way :You can call it like this :
And you’ll get :
Now, having a default value for a parameter, the code could look like this :
Note : I only added the default value for the parameter, in the first argument passed to create_function.
And, then, calling the new function :
I still get :
i.e. the default value for the parameter has been used.
So, default values for parameters work with
create_functionjust like they do for other functions : you just have to put the default value in the list of parameters.After that, on how to create the string containing the parameters and their values… A couple of string concatenations, I suppose, without forgetting to escape what should be escaped.