2.2.1 Problem
You want to ensure
that a string contains a number. For example, you want to validate an age that
the user has typed into a form input field.
2.2.2 Solution
if (is_numeric('five')) { /* false */ }
if (is_numeric(5)) { /* true */ }
if (is_numeric('5')) { /* true */ }
if (is_numeric(-5)) { /* true */ }
if (is_numeric('-5')) { /* true */ }
2.2.3 Discussion
Besides working on numbers, is_numeric( ) can also be applied to
numeric strings. The distinction here is that the integer 5 and the
string 5 technically aren't the same in PHP.[2]
[2] The most glaring example of this difference came during the transition from PHP 3 to PHP 4. In PHP 3, empty('0') returned false, but as of PHP 4, it returns true. On the other hand, empty(0) has always returned true and still does. (Actually, you need to call empty( ) on variables containing '0' and 0.) See the Introduction to Chapter 5 for details.
Helpfully, is_numeric( ) properly parses decimal
numbers, such as 5.1; however, numbers with thousands separators, such
as 5,100, cause is_numeric( ) to return false.
To strip the thousands separators from your number before
calling is_numeric( ) use str_replace( ):
is_numeric(str_replace($number, ',', ''));