I am using ‘filter_var()’ function with FILTER_CALLBACK.
Something like this:
$value = filter_var($value, FILTER_CALLBACK, array('options' => 'my_func'));
my_func() usually receives the $value as an argument:
function my_func($val)
{
if (some cond)
{
return $val;
}
else
{
return false;
}
}
Now I want to use another $var in my_func() from the outer scope (I mean with the filter_var()).
One option is to declare it as global in the function. Another is to pass it as argument.
How can I avoid declaring global and pass the $var as argument?
If you’re using PHP 5.3+, you could use closures:
Of course, this example doesn’t really “validate” anything, just changes the text a bit, but it should give you an idea of how it works.
EDIT: Judging from the comments, you want to use a certain function for certain values without checking which one to use at each loop. Have you considered using call_user_func? Here’s an example: