PHP is_numeric() function
The is_numeric()
function in PHP is used to determine whether a given variable is a number or a numeric string. It checks if the value can be interpreted as a number, either as an integer, float, or a numeric string.
Syntax:
Parameters:
- $value: The variable or value you want to check.
Return Value:
- true if the value is numeric (can be interpreted as a number).
- false if the value is not numeric.
Example 1: Checking Numeric Values
Output:
Example 2: Non-numeric Values
Output:
Example 3: Scientific Notation and Hexadecimal
Output:
Practical Usage:
is_numeric()
is useful when you need to validate user input or check if a value can be used in mathematical operations.- It helps ensure that variables are valid numbers before performing calculations to avoid errors or unexpected behavior.
Summary:
is_numeric()
checks whether a value is a valid number or numeric string.- It returns
true
for integers, floats, and numeric strings, andfalse
for non-numeric values.