Understanding the is_numeric() Function in PHP
  John Mwaniki /   16 Dec 2023

Understanding the is_numeric() Function in PHP

In this article, we'll look into what the is_numeric() function is in PHP, what it does, and its common use cases.

What is is_numeric()?

The is_numeric() function is a built-in function in PHP that evaluates whether a variable is a number or a numeric string.

Its primary purpose is to assist developers in validating user input or handling variables where the nature of the data might be ambiguous. By using it, one can ensure that their code operates with numeric values as intended, preventing unexpected errors or behaviors.

Syntax:

is_numeric($variable)

The function takes one argument, which is the variable being tested, and returns a boolean value (true if the variable is numeric, false otherwise).

Example

<?php
$number = 42;
if (is_numeric($number)) {
    echo $number. " is a numeric value<br>";
}
else {
    echo $number. " is not numeric<br>";
}

$numericString = "3.14";
if (is_numeric($numericString)) {
    echo $numericString. " is a numeric value<br>";
}
else {
    echo $numericString. " is not numeric<br>";
}

$nonNumeric = "Hello, World!";
if (is_numeric($nonNumeric)) {
    echo $nonNumeric. " is a numeric value";
}
else {
    echo $nonNumeric. " is not numeric";
}
?>

Output:
42 is a numeric value
3.14 is a numeric value
Hello, World! is not numeric

This function recognizes a wider range of numeric forms, including integers, floating-point numbers, exponents (numbers written in scientific notation, with a base and an exponent e.g., 2.5e6), positive/negative numbers (numbers prefixed with + or - (e.g., -150), and considers them as valid.

Example

<?php
$testvalues = array(56, "108", "4.273", "", "hello word", 2.5e6, null, "92.1e5", -34);

foreach ($testvalues as $value) {
    if (is_numeric($value)) {
        echo var_export($value, true). " is a numeric value<br>";
    }
    else {
        echo var_export($value, true). " is not numeric<br>";
    }
}
?>

Output:
56 is a numeric value
'108' is a numeric value
'4.273' is a numeric value
'' is not numeric
'hello word' is not numeric
2500000.0 is a numeric value
NULL is not numeric
'92.1e5' is a numeric value
-34 is a numeric value

In the above example, we placed a wide range of values in an array and iterated through them using the foreach() loop, testing each value with the is_numeric() and displaying whether it is a numeric value or not.

Starting with PHP v8.0.0, numeric strings ending with whitespace (eg "38 ") now returns true. Previously, false was returned in older versions.

Use Cases for is_numeric() Function

User Input Validation

One of the most common use cases for is_numeric() is in validating user input. When dealing with forms or user interactions, data often comes in as strings. The function allows developers to ensure that the input intended to be numeric indeed meets that criterion.

Pass the user's input to the function, and if it returns true, proceed with processing it as a number. Otherwise, you can display an error message.

Example

<?php
$userInput = $_POST['age'];

if (is_numeric($userInput)) {
    // Process the numeric input
}
else {
    echo "The age must be a number.";
}
?>

Database Operations

When working with databases, there is a likelihood of having some numeric values stored as strings in non-numeric field types such as VARCHAR. For instance, in the price field, some values might have been stored as "100" and others as "$100" or "USD100".

On retrieving values from such fields, it's important to confirm their numeric nature before performing arithmetic operations or comparisons.

Example

<?php
$price = $row['price'];

if (is_numeric($price)) {
    $total = $price * $quantity;
    // Continue with calculations
}
else {
    // Handle non-numeric value from the database
}
?>

Conclusion

The is_numeric() function in PHP comes in handy as a valuable tool for developers working with numeric data. Whether validating user input, processing database values, or dynamically adapting to changing variable types, this function adds a layer of assurance and flexibility to PHP code.