[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12690
  • Last Modified:

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.

1 Solution

Richard QuadlingSenior Software DeverloperCommented:
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().


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now