In this tutorial, we’ll talk about how to use is numeric in PHP to check the value of a string. With the help of examples, it’s easy to figure out what is_numeric
is used for.
Before we begin, if you missed any of our prior lessons, you can review the topics in our PHP Tutorial for Beginners.
Definition and Usage of is_numeric in PHP
The is_numeric()
function describes whether a variable is a number or a string of numbers. If the variable is a number or a numeric string, this function returns true; otherwise, it returns false/nothing.
Additionally, based on the PHP documentation, is_numeric
finds whether a variable is a number or a numeric string.
Syntax
is_numeric( variable );
Parameter Values
Parameter | Description |
---|---|
| Required. Specifies the variable to check |
Technical Details
Return Value: | TRUE if variable is a number or a numeric string, FALSE otherwise |
Return Type: | Boolean |
PHP Version: | 4.0+ |
Is_numeric PHP examples
The following is an example usage of is_numeric
:
<?php
// if it is a number
$str = "050122";
if ( is_numeric($str) ) {
echo "'{$str}' is a numeric.";
} else {
echo "'{$str}' is not a numeric.";
}
echo ("\n");
// if it is a string
$str = "itsourcecode";
if ( is_numeric($str) ) {
echo "'{$str}' is a numeric.";
} else {
echo "'{$str}' is not a numeric.";
}
?>
Output
'050122' is a numeric.
'itsourcecode' is not a numeric.
Explanation
The example above illustrates that if the value of a $str
value is a number, it will print that it is numeric. If it is a string, then it will print that it is not numeric.
Is_numeric PHP examples with whitespace
The following is an example usage of is_numeric
, but this time, the value of the string has whitespace:
<?php
// if it is a number
$str = "05 01 22";
if ( is_numeric($str) ) {
echo "'{$str}' is a numeric.";
} else {
echo "'{$str}' is not a numeric.";
}
echo ("\n");
// if it is a string
$str = "it source code";
if ( is_numeric($str) ) {
echo "'{$str}' is a numeric.";
} else {
echo "'{$str}' is not a numeric.";
}
?>
Output
'05 01 22' is not a numeric.
'it source code' is not a numeric.
Explanation
The example above illustrates that if the value of a $str
value is a number with whitespace, it will print that it is not numeric. Therefore, if the variable value has whitespace on it, it will return a value of False
.
Frequently Ask Question
What is_numeric value in PHP?
PHP has a lot of built-in functions that can be used to check the type of data in a variable. One of them is called is numeric(). It is used to see if a variable or a string of numbers is a number or a number.
Along with the digits, the string of numbers can have the “+/-” symbol, a decimal point, and an exponential part. This tutorial shows how this function can be used for different things in PHP.
Is_numeric array PHP?
With a numeric array, we can store more than one value of the same type in a single variable instead of making a new variable for each value. The index, which is always a number in a numeric array, can then be used to get to these values.
Note: The index always starts at 0
by default.
Is a function a numeric?
Returns a Boolean value that says whether an expression can be evaluated as a number. The required expression argument is a Variant with a string or numeric expression.
Related Articles
- PHP Preg_Replace with Advanced Examples
- PHP Array_Walk With Advanced Example
- Sort An Array PHP (With Example Programs)
- PHP Variable Types (Understanding Variable Types with Examples)
- PHP String (with Program Examples )
Summary
To summarize, we now understand exactly how to use is_numeric
. I hope this tutorial has helped you understand the meaning and application of is_numeric
in PHP.
Lastly, if you want to learn more about is_numeric
in PHP, please leave a comment below. We’ll be happy to hear it!