An example of why i don’t like the ext/filter API

Earlier this week i decided to experiment with the Filter functions. Here’s an example that illustrates why i think the API needs to be improved:

<?php
$isgoodapi = filter_input(INPUT_GET, 'isgoodapi', FILTER_VALIDATE_BOOLEAN);

if (is_null($isgoodapi)) { 
 echo "the 'isgoodapi' argument is missing.";
} else if ($isgoodapi === FALSE) { 
 echo "The 'isgoodapi' argument must be a valid boolean."; 
} else { 
 echo "isgoodapi is: $isgoodapi."; 
}
?>

And now you request the page with ?isgoodapi=false. The obvious problem is the fact that the function returns multiple ‘sorts’ of return values: Value of the requested variable on success, FALSE if the filter fails, or NULL if the variable_name variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not set and NULL if the filter fails.

The documentation for Filter Functions says for FILTER_VALIDATE_BOOLEAN: Returns TRUE for “1″, “true”, “on” and “yes”, FALSE for “0″, “false”, “off”, “no”, and “”, NULL otherwise. So if you try with ?isgoodapi=konijn you would expect NULL but that isn’t the case either.

This entry was posted on Wednesday, December 27th, 2006 at 03:11 and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.