In this post we will be creating a PHP script that will check if an existing variable is of an integer type, to do this we will be using the ‘is_int()’ 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(23, "101", 98, "Hello World", 192.4364, 192);
In the snippet of code below we are iterating the above array and using the ‘is_int()’ 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(23, "101", 98, "Hello World", 192.4364, 192);
foreach ($array as $item) {
echo $item . " - " ;
echo var_dump(is_int($item)) . "<br>";
}
?>
The above would return the following as output.
23 - bool(true)
101 - bool(false)
98 - bool(true)
Hello World - bool(false)
192.4364 - bool(false)
192 - bool(true)