isnumeric function for PHP

Hi is there an isnumeric or similar function available for PHP?

Values could be any numeric type just need to check that the string value doesn't actually contain characters.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Richard QuadlingSenior Software DeveloperCommented:
This is from the PHP Manual ...

bool is_numeric ( mixed var )

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

There is also is_int() and is_float()

Notes about is_int() and is_float() though ...

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

None of these functions return the numeric value, they just tell you that it IS a number/int/float.

Depending upon the exact format of the number (i.e. thousand separators, comma decimal place, etc), you will have problems ...

$as_test_strings = array
      '100', // Normal integer
      '3.1415', // Float
      '1,000,000', // With thousand separator
      '1.234.567,89', // With thousand separator and French , and .

foreach($as_test_strings as $s_test_string)
      echo $s_test_string . ' is ' . (is_numeric($s_test_string) ? '' : 'not ') . "numeric\n";

reports ...

100 is numeric
3.1415 is numeric
1,000,000 is not numeric
1.234.567,89 is not numeric

The French format is documented with an example in the PHP Function number_format().

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.