Whole number or decimal number

Should 10.0 be considered as an integer or a float? If integer, you're looking for fmod():

if (fmod($number, 1) == 0) // $number DOES NOT have a significant decimal part
{
    // is whole number
}

Otherwise, is_int() suffices.


EDIT: Another way to check for insignificant decimal part would be:

if (round($number, 0) == $number)
{
    // is whole number
}

is_numeric will return true for floats too, considering floats are numeric

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

You can try is_float(), but if the input is a string it wont work.

var_dump( is_float( '23.5' ) ); // return false

So if you are dealing with something that is a string representation of a number then just look for a .

if ( strpos( $answer3, '.' ) === false )

You can add is_numeric if you need to

// Make sure its numeric and has no decimal point
if ( is_numeric( $answer3 ) && strpos( $answer3, '.' ) === false )

Tags:

Php