How to Use the PHP Is_Numeric () Function

Posted by

How to Use the PHP Is_Numeric () Function

The is_numeric () function in PHP programming language which is used to check your value which is a number or a numeric string. Numeric strings which contain any number of optional signs, digits such as + or _ and has optional decimal and optional exponential. As there is +234.5e6 is a valid numeric string. Binary notation and hexadecimal notation are not allowed.

The is_numeric () function must 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

Similar function, ctype digit which checks for numeric characters but only for digits which has 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

Leave a Reply

Your email address will not be published. Required fields are marked *