W3docs

Check if number is decimal

In PHP, you can use the is_float() function to check if a variable is a decimal number.

In PHP, you can use the is_float() function to check if a variable is a decimal number.

Example:

Example of using the is_float() function to check if a variable is a decimal number in PHP

<?php

$num = 3.14;
if (is_float($num)) {
  echo '$num is a decimal number';
} else {
  echo '$num is not a decimal number';
}

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

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.

Example of using the fmod() function to check if a variable is a decimal number in PHP

<?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

Example of using the filter_var() function to check if a variable is a decimal number in PHP

<?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';
}