First I'll show the function:
function validate($data, $filter){
if(!filter_var($data, $filter))
{
return 'Filter used:'.$filter;
}
else
{
return 'good';
}
}
When I use this function it will look like this:
$email = '[email protected]';
echo validate($email, FILTER_VALIDATE_EMAIL);
Output is:
Filter used: 274
The problem is that I want the output to be:
Filter used: FILTER_VALIDATE_EMAIL.
FILTER_VALIDATE_EMAIL
is a named constant that is set to274
. Yourvalidate()
function never sees "FILTER_VALIDATE_EMAIL", it just sees274
being passed into it and passes274
tofilter_var()
, which knows how to handle it.One advantage of constants for the developer is: by using an IDE with auto-complete and code intelligence, you can be certain of the variable's name and thus can avoid possible typos.
Look at @AbraCadaver's answer for an elegant solution to your query.