PHP is numeric

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : is_numeric() function

Description

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

Version

(PHP 4 and above)

Syntax

is_numeric (var_name)

Parameter

Name

Description

Required /
Optional

Type

var_name

The variable being checked

Required

Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

TRUE if var_name is a number or a numeric string, FALSE otherwise.

Value Type : Boolean.

Example :

  1. $var_name1=678;  

  2. $var_name2="a678";  

  3. $var_name3="678";  

  4. $var_name4="W3resource.com";  

  5. $var_name5=698.99;  

  6. $var_name6=array("a1","a2");  

  7. $var_name7=+125689.66;  

  8. if (is_numeric($var_name1))  

  9. {  

  10. echo "$var_name1 is Numeric.
    " ;  

  11. }  

  12. else  

  13. {  

  14. echo "$var_name1 is not Numeric. 
    " ;  

  15. }  

  16. if (is_numeric($var_name2))  

  17. {  

  18. echo "$var_name2 is Numeric.
    " ;  

  19. }  

  20. else  

  21. {  

  22. echo "$var_name2 is not Numeric.
    " ;  

  23. }  

  24. $result=is_numeric($var_name3);  

  25. echo "[ $var_name3 is numeric? ]" .var_dump($result)."
    ";  

  26. $result=is_numeric($var_name4);  

  27. echo "[ $var_name4 is numeric? ]" .var_dump($result)."
    ";  

  28. $result=is_numeric($var_name5);  

  29. echo "[ $var_name5 is numeric? ]" .var_dump($result)."
    ";  

  30. $result=is_numeric($var_name6);  

  31. echo "[ $var_name6 is numeric? ]" .var_dump($result)."
    ";  

  32. $result=is_numeric($var_name7);  

  33. echo "[ $var_name7 is numeric? ]" .var_dump($result);  

  34. ?>  

Output :

678 is Numeric.
a678 is not Numeric.
bool(true) [ 678 is numeric? ]
bool(false) [ W3resource.com is numeric? ]
bool(true) [ 698.99 is numeric? ]
bool(false) [ Array is numeric? ]
bool(true) [ 125689.66 is numeric? ]