How to Use the PHP Is_Numeric() Function

Use the Is_Numeric() function to check if a PHP variable is a number

Businesswoman typing on laptop
Paul Bradbury/OJO Images/Getty Images

The is_numeric() function in the PHP programming language is used to evaluate whether a value is a number or numeric string. Numeric strings contain any number of digits, optional signs such as + or -, an optional decimal, and an optional exponential. Therefore, +234.5e6 is a valid numeric string. Binary notation and hexadecimal notation are not allowed. 

The is_numeric() function can be used within an if() statement to treat numbers in one way and non-numbers in another. It returns true or false.

Examples of the Is_Numeric() Function

For example:

 <?php if (is_numeric(887)) { echo "Yes"; } else { echo "No"; } ?>

Because 887 is a number, this echos Yes. However:

 <?php if (is_numeric("cake")) { echo "Yes"; } else { echo "No"; } ?>

Because cake is not a number, this echos No.

Similar Functions

A similar function, ctype-digit(), also checks for numeric characters, but only for digits—no optional signs, decimals, or exponents allowed. Every character in the string text must be a decimal digit for the return to be true. Otherwise, the function returns false.

Other similar functions include:

  • is_null() – Finds whether a variable is NULL
  • is_float() – Finds whether the type of a variable is float
  • is_int() – Find whether the type of a variable is integer
  • is_string() – Find whether the type of a variable is string
  • is_object() – Finds whether a variable is an object
  • is_array() – Finds whether a variable is an array
  • is_bool() – Finds out whether a variable is a boolean

About PHP

PHP is an abbreviation for Hypertext Preprocessor. It is an open-source HTML-friendly scripting language that is used by website owners to write dynamically generated pages. The code is executed on the server and generates HTML, which is then sent to the client. PHP is a popular server-side language that can be deployed on almost every operating system and platform.