Reputation: 739
Pls see my code below, which is meant to validate whether a variable has on of the specific value (and based on solution example of Is it possible to add to filter_var() function user-defined parameters?)
I tested two cases (A, B) as descibed in the comments in the code below. I dont understand why the function is not working properly in case B? (which I tested by means of ideone.com)
function validate_select($val, $myoptions)
{
//print_r($myoptions);
for($i=0;$i<count($myoptions);$i++){
if($val==$myoptions[$i]){
return $val;
}
}
return false;
}
$testVar = 'apple';
$myoptions = array('banana','pear','apple');
$result = filter_var($testVar, FILTER_CALLBACK, array('options' => function($var) {
//return validate_select($var, array('banana','pear','apple')); //case A: returns correct value 'apple'
return validate_select($var, $myoptions); //case B: returns unexpected value false
}));
echo($result);
Upvotes: 0
Views: 38
Reputation: 1847
$myoptions
is outside of your function... try add function ($var) use ($myoptions)
like
$result = filter_var($testVar, FILTER_CALLBACK, array('options' => function ($var) use ($myoptions) {
return validate_select($var, $myoptions);
}));
Upvotes: 2