How to Check a Valid Number In PHP?

Problem:

You have a variable that contains a value. You want to check whether the value is a valid number or not.

Solution:

is_numeric() function in PHP can check whether a variable contains a valid number.

Example:

<?php
$var1 = 5;
echo "is_numeric($var1) = " . ( is_numeric($var1) ? "True" : "False" ) . "<br />";
$var2 = ‘5’;
echo "is_numeric($var2) = " . ( is_numeric($var2) ? " True " : " False " ) . "<br />";
$var3 = 5.5;
echo "is_numeric($var3) = " . ( is_numeric($var3) ? " True " : " False " ) . "<br />";
$var4 = true;
echo "is_numeric($var4) = " . ( is_numeric($var4) ? " True " : " False " ) . "<br />";
$var5 = ‘hello’;
echo "is_numeric($var5) = " . ( is_numeric($var5) ? " True " : " False " ) . "<br />";
?>

Output:
is_numeric(5) = True
is_numeric(‘5’) = True
is_numeric(5.5) = True
is_numeric(true) = False
is_numeric(‘hello’) = False

How it works:

Line 3 $var1 contains 5 which is a number; so, the function is_numeric() returns true.
Line 5 $var2 contains ‘5’ which is a numeric string; so, The function is_numeric() returns True. Note, the function is_numeric() also returns true if the variable is a numeric string like the example on line 5.
Line 7 $var3 contains 5.5 which is a floating point number; so the function returns True
Line 9 $var4 contains true which is a boolean number; so, the function returns False
Line 11 $var1 contains ‘hello’ which is a string; so the function returns False

The example uses ternary operator (?). In case you don’t know click here to know how ternary operator works.

You may come across with a number like 10,000,000 which you want to consider as a valid number. For the presence of the thousands separators, the function is_numeric() doesn’t consider it as a valid number. To make a valid number remove the thousands separator with the str_replace() function. Check the following example.

Example:

<?php
$var1 = “10,000,000”;
echo "is_numeric($var1) = " . ( is_numeric($var1) ? "True" : "False" ) . "<br />";
echo "is_numeric(str_replace(',', '', $var1)) = " . ( is_numeric(str_replace(',', '', $var1)) ? "True " : "False " ) . "<br />";
?>

Output:
is_numeric(10,000,000) = False
is_numeric(str_replace(‘,’, ”, 10,000,000)) = True

How it Works:

Line 4 str_replace() function replaces thousands separators(comma which is its first parameter) by nothing(‘’ which is second parameter) in the 10,000,000 and makes it 10000000 which is a valid number. So, is_number() function returns True.

is_numeric() function at a glance

(PHP4, PHP5)
Usages:
file_exists() function checks whether a value is a number or numeric string. If it is a valid number or numeric string it returns true otherwise returns false.

Syntax:
is_numeric(variable_name)

Parameters:

Parameter What it does
variable_name Required The variable to be checked.

More Examples

Example 1:

<?php
$var1 = “2E6”;
echo "is_numeric($var1) = " . ( is_numeric($var1) ? "True" : "False" ) . "<br />";
$var2 = “10e4”;
echo "is_numeric($var2) = " . ( is_numeric($var2) ? " True " : " False " ) . "<br />";
?>

Output:
is_numeric(2E6) = False
is_numeric(10e4) = True

How it works:

Line 3 $var1 contains 2E6 which is a hexadecimal number; so, the function is_numeric() returns true.
Line 5 $var2 contains 10e4 which is a scientific number; so, The function is_numeric() returns True.

  1. is_numeric() function returns true for hexadecimal and scientific numbers.
  2. Hexadecimal (2E6) = Decimal (2×162 + 14×161 + 6×160 = 2x16x16 + 14×16 + 6×1 = 742)
  3. Scientific number (10e4) = Decimal (10000)