Definitely, a good epilogue for this tutorial consists of showing you how to use the filter extension to check whether or not the value assigned to a PHP variable is a float number. Of course, if you were a careful reader and paid close attention to the examples developed in preceding articles of the series, then you may imagine that this task will be performed through another constant, called FILTER_VALIDATE_FLOAT.
This constant, when used with the "filter_var()" function, permits you to validate floating numbers very simply. The following code samples demonstrate how to check float numbers using a single scalar variable and an array as well. Here they are:
(basic example on validating float numbers)
$floatValue = 1.23456;
if(filter_var($floatValue, FILTER_VALIDATE_FLOAT) === FALSE) // displays Input is a valid float number.
echo 'Input is not a valid float number.';
echo 'Input is a valid float number.';
(example on validating float numbers using an array of values)
$values= array(10.75, '10.75', -1234.5678, 'This is a string', array());
$filteredValues = filter_var($values, FILTER_VALIDATE_FLOAT, FILTER_REQUIRE_ARRAY);
displays the following
0 => float 10.75
1 => float 10.75
2 => float -1234.5678
3 => boolean false
That was really simple to understand, wasn't it? As shown above, the filter extension doesn't care if the value being validated is a string or a real float number as long as it contains a decimal part. This was clearly demonstrated by the first example. With reference to the second one, it simply shows how to perform the same checking process using a trivial array.
However, the functionality of the FILTER_VALIDATE_FLOAT filter doesn't stop here. It has the ability to specify what type of decimal notation will be used for validating float numbers. Does this sound a bit confusing? Fear not. Take a look at the following example, which should help to dissipate any possible doubts you might have on this:
(example on validating float numbers using a separator)
$values= array(10.75 => '.', '10,75' => ',');
foreach ($values as $value => $separator)
$filteredValue = filter_var($value, FILTER_VALIDATE_FLOAT, array('options' => array('decimal' => $separator)));
/* displays the following
Here you have it. Now, thanks to the help of an additional "options" parameter, it's possible to specify which character will be used for coding the decimal part of a float number. This can be really useful if you're developing a web application that will deal with different decimal notations.
And that's all for now regarding the use of the PHP filter extension for checking float numbers. As always, feel free to edit all of the code samples included in this article; in this way you can sharpen your skills even further when working with this powerful library.
Over this fourth chapter of the series, I discussed the use of the FILTER_VALIDATE_FLOAT filter for validating float numbers in a truly straightforward fashion. Undeniably, what makes the filter extension so easy to work with is that in most cases, validating a specific type of data only requires coupling the "filter_var()" function with the corresponding filtering constant. It's that simple, really.
Moving forward, in the upcoming part I'm going to explore in depth another filter provided by the extension. It will be used to check whether the value assigned to a variable follows the pattern defined by a specified regular expression. Don't miss the next part!
blog comments powered by Disqus