W3docs

is_numeric()

The is_numeric() function is a built-in function in PHP that checks whether a variable is numeric or not. A numeric value is a value that can be represented as

Introduction

is_numeric() is a built-in PHP function that tests whether a value is a number or a numeric string. That last part is the key reason it exists: PHP often receives numbers as strings — from form fields, query parameters, JSON, CSV files — and you need a reliable way to ask "could this be treated as a number?" before you do arithmetic on it. is_numeric() answers exactly that question and returns a boolean.

This page covers the syntax, what counts as numeric (and what surprisingly does not), how it differs from is_int() and is_float(), and a practical input-validation pattern.

Syntax

is_numeric(mixed $value): bool

It takes a single argument, $value, and returns true if $value is a number or a numeric string, and false otherwise. It never throws and never modifies its argument.

Basic example

php— editable, runs on the server

$var1 is the string "42", $var2 is the float 3.14, and $var3 is the string "hello". The first two are numeric, so is_numeric() returns true; the third is not.

Tip: Use var_dump() rather than echo when testing booleans. echo prints true as "1" and false as an empty string, which is easy to misread. var_dump() prints bool(true) / bool(false) so the result is unambiguous.

What counts as numeric

A string is numeric when it represents a valid integer or float in decimal or scientific notation. This includes a leading sign, a leading or trailing decimal point, surrounding whitespace, and exponents:

<?php
var_dump(is_numeric("1e3"));    // bool(true)  — scientific notation (1000)
var_dump(is_numeric("+42"));    // bool(true)  — leading sign
var_dump(is_numeric("-0.5"));   // bool(true)  — negative float
var_dump(is_numeric(".5"));     // bool(true)  — leading decimal point
var_dump(is_numeric("  42"));   // bool(true)  — leading whitespace

var_dump(is_numeric("0x1A"));   // bool(false) — hex strings are NOT numeric (since PHP 7)
var_dump(is_numeric("0b101"));  // bool(false) — binary strings are not numeric
var_dump(is_numeric(""));       // bool(false) — empty string
var_dump(is_numeric("42px"));   // bool(false) — trailing non-numeric characters
?>

The common gotchas:

  • Hex and binary strings return false. "0x1A" was numeric before PHP 7 but is not anymore. Numeric literals written in code (e.g. 0x1A) are already real integers, so is_numeric() sees a plain int and returns true.
  • A trailing unit makes the whole string non-numeric. "42px" is false. If you need to pull a number out of such a string, use (int) casting or filter_var() instead.
  • Leading/trailing whitespace is tolerated (trailing whitespace became allowed in PHP 8.0).

is_numeric() vs. is_int() and is_float()

These three functions are easy to confuse. The difference is whether the type matters or only the value:

<?php
$value = "10";          // a string that looks like a number

var_dump(is_numeric($value)); // bool(true)  — value can be a number
var_dump(is_int($value));     // bool(false) — type is string, not int
var_dump(is_float($value));   // bool(false) — type is string, not float
?>
  • is_numeric() cares about the value: "could this be a number?" It accepts numeric strings.
  • is_int() and is_float() care about the declared type: they only return true for an actual int or float, never for a string.

Reach for is_numeric() when validating input that arrives as text; reach for is_int() / is_float() when you genuinely need to know a variable's type.

Practical use: validating input before math

The typical real-world job for is_numeric() is guarding arithmetic on untrusted input so you don't get TypeErrors or silent 0s:

<?php
$inputs = ["100", "12.5", "5e2", "abc", "12px", ""];

foreach ($inputs as $in) {
    if (is_numeric($in)) {
        echo "$in is numeric -> " . ($in + 0) . "\n";
    } else {
        echo "$in is NOT numeric\n";
    }
}
// 100 is numeric -> 100
// 12.5 is numeric -> 12.5
// 5e2 is numeric -> 500
// abc is NOT numeric
// 12px is NOT numeric
//  is NOT numeric
?>

Adding + 0 (or casting with (int) / (float)) converts the validated string to a real number once you know it is safe to do so. For stricter validation — for example, accepting integers only or applying a range — combine it with filter_var() and the FILTER_VALIDATE_INT / FILTER_VALIDATE_FLOAT filters.

Conclusion

is_numeric() reliably tells you whether a value — including a string — can be treated as a number, which makes it the right guard before doing arithmetic on data from forms, URLs, or files. Remember that it returns false for hex/binary strings and for any string with non-numeric trailing characters, and that it checks value, not type — use is_int() or is_float() when the declared type is what matters. See also gettype() and PHP Data Types for the bigger picture.

Practice

Practice
What does the is_numeric function in PHP do?
What does the is_numeric function in PHP do?
Was this page helpful?