Showing posts with label is_numeric(). Show all posts
Showing posts with label is_numeric(). Show all posts

is_int() and is_string() Do Not Work As Expected In PHP

is_int() returns TRUE if the variable passed in is an integer, which may sound similar to is_numeric(). However, data passed in through a form or from MySQL query, even if numeric in content, is of type string, which means that is_int() will fail. Is_numeric(), on the other hand, returns true if the variable is a number or if it is a string containing a number also. This same problem applies to is_float(), as floating-point values set from user input are typed as strings. On contrast, when numeric data passed through a form or from MySQL query, is_string() does return TRUE.

This feature makes is_int() and is_string() next to useless.


http://www.tuxradar.com/practicalphp/7/7/3

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

is_numeric() And is_int() in PHP

Both is_numeric() and is_int() will return neither TRUE nor FALSE, but null if parameter is null. Logically, it is correct. Practically, it does make these two functions next to useless. Because we human being would never believe null is numeric. For human being, it is obvious...

To make it useful, we would first consider if parameter is null or not, then if it is numeric. Here is simple way to test these three situations once for all:

if (0!=strlen(trim(@$para)) && is_numeric(@$para)) {...};

Labels