The is_numeric() function is used to detect whether a variable is a number or a numeric string.
PHP version requirements: PHP 4, PHP 5, PHP 7
grammar
bool is_numeric (mixed $var)
Parameter description:
$var: The variable to be tested.
return value
Returns TRUE if the specified variable is a number or numeric string, otherwise returns FALSE.
Example
Example
<?php $var_name1 = 678 ; $var_name2 = " a678 " ; $var_name3 = " 678 " ; $var_name4 = " codercto.com " ; $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 ; } $result = is_numeric ( $var_name3 ) ; echo " [ Is
$var_name3 a number? ] " . var_dump ( $result ) . PHP_EOL ; $result = is_numeric ( $var_name4 ) ; echo " [ Is
$var_name4 a number? ] " . var_dump ( $result ) . PHP_EOL ; $result = is_numeric ( $var_name5 ) ; echo " [ Is
$var_name5 a number? ] " . var_dump ( $result ) . PHP_EOL ; $result = is_numeric ( $var_name6 ) ; echo " [ Is
$var_name6 a number? ] " . var_dump ( $result ) . PHP_EOL ; $result = is_numeric ( $var_name7 ) ; echo " [ Is $var_name7 a number? ]
" . var_dump ( $result ) ; ?> The output is:
678 is a number a678 is not a number bool(true)[ Is 678 a number? ]bool(false)[ codercto.com Is it a number? ]bool(true)[ Is 698.99 a number? ]bool(false)[ Array Is it a number? ? ]bool(true)[ Is 125689.66 a number? ]