Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

is_numeric

Checks whether a value is a number or a numeric string.

Syntax

is_numeric(mixed $value): bool

Parameters

ParameterTypeDescription
$valuemixedThe value to check.

Return value

bool`true` for numbers and strings that look like a number.

Examples

Numeric and non-numeric strings:

<?php
 
var_dump(is_numeric('42'));
var_dump(is_numeric('4.5'));
var_dump(is_numeric('4 kg'));

Output:

bool(true)
bool(true)
bool(false)

Checking before a calculation:

<?php
 
$input = '15';
 
echo is_numeric($input) ? $input * 2 : 'not a number';

Output:

30

Notes

The function also accepts forms such as 1e5 and -0.5. If you need exactly an integer, use filter_var($value, FILTER_VALIDATE_INT).

See also

  • intval — Converts a value to an integer.
  • gettype — Returns the name of a value's type.
  • filter_var — Validates or sanitises a value by a given rule.

Related recipes

In the tutorial

  • Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.

Thanks for reading!

Donate to the site — we work hard for you.