In this post we will be creating PHP script that will check if an existing variable is of a float type, to do this we will be using the ‘is_float()’ function. For this example our variables will be stored in an array and we will carry out a check on each item within.
See our array of items for this example below.
$array = array(26.132, "10.243", 98.0000, "Aloha", 192.4364, 192);
In the snippet of code below we are iterating the above array and using the ‘is_float()’ function to carry out our check. The True or False values will be printed beside the item and each item will be printed on a new line.
<?php
$array = array(26.132, "10.243", 98.0000, "Aloha", 192.4364, 192);
foreach ($array as $item) {
echo $item . " - " ;
echo var_dump(is_float($item)) . "<br>";
}
?>
The above would return the following as output.
26.132 - bool(true)
10.243 - bool(false)
98 - bool(true)
Aloha - bool(false)
192.4364 - bool(true)
192 - bool(false)