How to Use the PHP Is_Numeric () Function

Check whether a PHP variable is a number using Is_Numeric ()

Format
mla apa chicago
Your Citation
Bradley, Angela. "How to Use the PHP Is_Numeric () Function." ThoughtCo, Dec. 5, 2016, thoughtco.com/isnumeric-php-function-2694075. Bradley, Angela. (2016, December 5). How to Use the PHP Is_Numeric () Function. Retrieved from https://www.thoughtco.com/isnumeric-php-function-2694075 Bradley, Angela. "How to Use the PHP Is_Numeric () Function." ThoughtCo. https://www.thoughtco.com/isnumeric-php-function-2694075 (accessed September 22, 2017).
Businesswoman typing on laptop
Paul Bradbury/OJO Images/Getty Images

The is_numeric () function in the PHP programming language is used to check if 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. 

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.

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 HTML-friendly scripting language that is often 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 development began in the mid-90s, and it has become a popular server-side language that can be deployed on almost every operating system and platform.