Is_numeric() Is Not Good Enough in PHP

It supposes good enough. However, it fails in following circumstances:

1. In some machines number 0 returns false.
2. Unable to detect ‘+.1’ as numeric.
3. When number flagged as string in database, it returns true. Well, it is not in_numeric’s fault since it is designed that way.
4. However, when six digits number is hexadecimal number for color code in HTML, it would for sure stored in database as string, which does cause the problem.

Solutions:
For 1, If (is_numeric($para) || 0==$para))
For 2, no simple solution as good as enough
For 3, no solution
For 4, store hexadecimal number with # in front in database.


http://stackoverflow.com/questions/2774472/php-is-numeric-returns-false-on-0

No comments:

Post a Comment

Labels