is_numeric

is_numeric(mixed $value): bool

Finds whether a variable is a number or a numeric string.

C C23

C23
#include <stdio.h>
#include <stdlib.h>

static int is_numeric(const char *s) {
    char *end;
    (void)strtod(s, &end);
    return *s != '\0' && *end == '\0';
}

int main(void) {
    printf("%d %d %d\n", is_numeric("42"), is_numeric("1e3"), is_numeric("abc"));
    return 0;
}
Output
1 1 0

C++ C++26

C++26
#include <iostream>
#include <string>

static bool is_numeric(const std::string &s) {
    try {
        (void)std::stod(s);
        return true;
    } catch (const std::exception &) {
        return false;
    }
}

int main() {
    std::cout << is_numeric("42") << " " << is_numeric("1e3") << " " << is_numeric("abc") << "\n";
    return 0;
}
Output
1 1 0

C# v11.0.100

v11.0.100
static bool IsNumeric(string s) => double.TryParse(s, out _);

Console.WriteLine($"{IsNumeric("42")} {IsNumeric("1e3")} {IsNumeric("abc")}");
Output
True True False

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "strconv"
)

func isNumeric(s string) bool {
    _, err := strconv.ParseFloat(s, 64)
    return err == nil
}

func main() {
    fmt.Println(isNumeric("42"), isNumeric("1e3"), isNumeric("abc"))
}
Output
true true false

Java v27.0.1

v27.0.1
public class Main {
    private static boolean isNumeric(String s) {
        try {
            Double.parseDouble(s);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(isNumeric("42") + " " + isNumeric("1e3") + " " + isNumeric("abc"));
    }
}
Output
true true false

JavaScript v26.13.1

v26.13.1
function isNumeric(v) {
  return v !== "" && Number.isFinite(Number(v));
}
console.log([isNumeric(42), isNumeric("1e3"), isNumeric("abc")]);
Output
[ true, true, false ]

PHP v8.6.0

v8.6.0
echo json_encode([is_numeric(42), is_numeric("1e3"), is_numeric("abc")]);
Output
[true,true,false]

Python v3.15.2

v3.15.2
def is_numeric(value):
    try:
        float(value)
        return True
    except (TypeError, ValueError):
        return False

print([is_numeric(42), is_numeric("1e3"), is_numeric("abc")])
Output
[True, True, False]

Rust v1.99.1

v1.99.1
fn is_numeric(s: &str) -> bool {
    s.parse::<f64>().is_ok()
}

fn main() {
    println!("{} {} {}", is_numeric("42"), is_numeric("1e3"), is_numeric("abc"));
}
Output
true true false

TypeScript v26.13.1

v26.13.1
function isNumeric(v: unknown): boolean {
  return v !== "" && Number.isFinite(Number(v));
}
console.log([isNumeric(42), isNumeric("1e3"), isNumeric("abc")]);
Output
[ true, true, false ]

Language differences

C: strtod parses a prefix and leaves trailing characters — check the end pointer to ensure the entire string is numeric.

C++: std::stod parses a prefix and leaves trailing characters — an exception must be checked to ensure the entire string is numeric.

C#: uses double.TryParse.

Go: uses strconv.ParseFloat.

Java: uses Double.parseDouble plus an exception handler.

JavaScript: no built-in — uses Number.isFinite(Number(v)) (edge cases like Number(null) and empty strings need explicit exclusion).

PHP: is_numeric accepts int/float and numeric strings in scientific or hexadecimal notation.

Python: the common equivalent is "try parsing the whole string" — a hand-written try-float helper.

Rust: uses str::parse::<f64>().

TypeScript: same as JS — Number.isFinite(Number(v)).