Easy Tutorial
❮ Func Mysqli Connect Error Php If Else ❯

PHP is_numeric() Function

PHP Available Functions

The is_numeric() function is used to check whether a variable is a number or a numeric string.

PHP Version Requirements: PHP 4, PHP 5, PHP 7

Syntax

bool is_numeric ( mixed $var )

Parameter Explanation:

Return Value

Returns TRUE if the specified variable is a number or a numeric string, otherwise returns FALSE. Note that floating-point numbers return 1, which is TRUE.

Example

<?php
$var_name1 = 678;
$var_name2 = "a678";
$var_name3 = "678";
$var_name4 = "tutorialpro.org";
$var_name5 = 698.99;
$var_name6 = array("a1", "a2");
$var_name7 = +125689.66;
if (is_numeric($var_name1))
{
    echo "$var_name1 is a number" . PHP_EOL;
}
else
{
    echo "$var_name1 is not a number" . PHP_EOL;
}
if (is_numeric($var_name2))
{
    echo "$var_name2 is a number" . PHP_EOL;
}
else
{
    echo "$var_name2 is not a number" . PHP_EOL;
}
echo '---------------------' . PHP_EOL;
$result = is_numeric($var_name3);
echo "[ $var_name3 is a number? ]" . var_dump($result) . PHP_EOL;
echo '---------------------' . PHP_EOL;
$result = is_numeric($var_name4);
echo "[ $var_name4 is a number? ]" . var_dump($result) . PHP_EOL;
echo '---------------------' . PHP_EOL;
$result = is_numeric($var_name5);
echo "[ $var_name5 is a number? ]" . var_dump($result) . PHP_EOL;
echo '---------------------' . PHP_EOL;
$result = is_numeric($var_name6);
echo "[ $var_name6 is a number? ]" . var_dump($result) . PHP_EOL;
echo '---------------------' . PHP_EOL;
$result = is_numeric($var_name7);
echo "[ $var_name7 is a number? ]" . var_dump($result);
?>

Output:

678 is a number
a678 is not a number
---------------------
bool(true)
[ 678 is a number? ]
---------------------
bool(false)
[ tutorialpro.org is a number? ]
---------------------
bool(true)
[ 698.99 is a number? ]
---------------------
bool(false)
[ Array is a number? ]
---------------------
bool(true)
[ 125689.66 is a number? ]

PHP Available Functions

❮ Func Mysqli Connect Error Php If Else ❯