Check if number is decimal
In PHP, you can use the is_float()
function to check if a variable is a decimal number.
Example:
<?php
$num = 3.14;
if (is_float($num)) {
echo '$num is a decimal number';
} else {
echo '$num is not a decimal number';
}
Watch a video course
Learn object oriented PHP
Another way is using the fmod()
function, which returns the remainder of dividing two numbers. If the remainder is not zero, it means that the number is decimal.
<?php
$num = 3.14;
if (fmod($num, 1) !== 0.0) {
echo '$num is a decimal number';
} else {
echo '$num is not a decimal number';
}
or use filter_var()
function
<?php
$num = 3.14;
if (filter_var($num, FILTER_VALIDATE_FLOAT)) {
echo '$num is a decimal number';
} else {
echo '$num is not a decimal number';
}