I am writing a REST interface with PHP to submit and query data coming from a weather station. Windspeed/temperature/etc values come via POST request as float (e.g. 1.32) unless they are zero, in which case the single '0' digit is sent.
I have written the following code to verify input values:
if (is_numeric($params['wind']))
{
if (!filter_var($params['wind'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION, $windLimit))
{
throw new Exception('WIND (wind) WRONG VALUE TYPE [received '.$params['wind'].'].');
}
}
else
{
throw new Exception('WIND (wind) WRONG TYPE [received '.$params['wind'].'].');
}
which works well for non-zero values, but fail verification when exactly 0. Is there a specific filter to be used in filter_var() when value can both be integer or float?
In order to allow for zero values use === FALSE
(i.e strictly equal to false):
if (filter_var(...) === FALSE)
{
throw new Exception('WIND (wind) WRONG VALUE TYPE [received '.$params['wind'].'].');
}