The is_numeric() function is an inbuilt function in PHP which is used to check whether a variable passed in function as a parameter is a number or a numeric string or not. The function returns a boolean value.
Syntax:
bool is_numeric ( $var )
Parameters: The function accepts a single parameter which is mandotary and described below:
- $var: This input parameter is the variable which the function checks for whether it is a number or a numeric string. Based on this verification, the function returns a boolean value.
Return Value: The function returns TRUE if $var is a number or a numeric string and returns FALSE otherwise.
Examples:
Input : $var = 12 Output : True Input : $var = "Geeks for Geeks" Output : False
Below programs illustrate the is_numeric() function:
Program 1: In this program, a number is passed as input.
<?php $num = 12;
if (is_numeric($num)) {
echo $num . " is numeric";
}
else {
echo $num . " is not numeric";
}
?> |
12 is numeric
Program 2: In this program, a string is passed as input.
<?php $element = "Geeks for Geeks";
if (is_numeric($element)) {
echo $element . " is numeric";
}
else {
echo $element . " is not numeric";
}
?> |
Geeks for Geeks is not numeric
Program 3: In this program, a numeric string is passed as input.
<?php $num = "467291";
if (is_numeric($num)) {
echo $num . " is numeric";
}
else {
echo $num . " is not numeric";
}
?> |
467291 is numeric
Program 4:
<?php $array = array(
"21/06/2018",
4743,
0x381,
01641,
0b1010010011,
"Geek Classes"
); foreach ($array as $i) {
if (is_numeric($i)) {
echo $i . " is numeric"."\n";
} else {
echo $i . " is NOT numeric"."\n";
}
} ?> |
21/06/2018 is NOT numeric 4743 is numeric 897 is numeric 929 is numeric 659 is numeric Geek Classes is NOT numeric
Reference: http://php.net/manual/en/function.is-numeric.php
Recommended Posts:
- JQuery | isNumeric() method
- Underscore.js _.isNumeric() Method
- Lodash _.isNumeric() Method
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

