In case you still havenít read the previous installment of this series, where I discussed the usage of the FILTER_VALIDATE_FLOAT filter for checking float numbers, hereís the group of code samples I developed in that article. They demonstrate how to work with it, in conjunction with the already familiar ďfilter_var()Ē function. Take a look at them, please:
(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
(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
As depicted by the above examples, the filter extension makes it really easy to validate float numbers using the FILTER_VALIDATE_FLOAT filter. As you can see, the first case shows how to use the filter to check a single float number, while the second code sample is a bit more complex, since it demonstrates how to check floating numbers stored in a trivial array.
Finally, the last example is perhaps the most useful of all, because it illustrates the usage of the ďoptionsĒ argument. This arguments permits you to specify which decimal delimiter will be utilized when validating a given float number. That was really simple to grasp, wasnít it?
So far, everything should look pretty good to you; you've hopefully recalled how to work with the FILTER_VALIDATE_FLOAT filter in different cases. Considering this, itís time to explore other useful validation capabilities given by the PHP 5 filter extension.
So, in accordance with the concepts I deployed in the introduction, in the course of the following segment Iím going to explain how to use another filter that comes with this extension. As you'll see in a few moments, it's useful for determining if the value assigned to a variable follows a pattern defined through a regular expression.
This new topic will be covered in detail in the next section. So, to learn more about it, please click on the link shown below and keep reading.
blog comments powered by Disqus