Matthew Sims wrote:

I recently purchased George Schlossnagle's Advanced PHP Programming and on
page 85 in the Error Handling chapter, he made a reference about the
is_int function.

In the above function example he had:
if (!preg_match('/^\d+$/',$n) || $n < 0) {....

In which he mentions:
It might be strange to choose to evaluate whether $n is an integer by
using a regular expression instead of the is_int function. The is_int
function, however, does not do what you want. It only evaluates whether $n
has been typed as a string or as an integer, not whether the value of $n
is an integer.

Can anyone comment on this?

The value 5.5 passed form a text input in a form, for example, will fail is_int() (because it's actually a string) and pass is_numeric(), but you still don't know whether the value passed was (or can be) an integer.


If you want to just ensure the value IS an integer (without really caring what it's value is), then you can just cast it to an integer.

$n = (int)$n;

If it wasn't an integer to begin with, then it'll normally be zero (exceptions are strings that begin with numbers "5abc" will have the value 5, but again, this is if you really don't care what the value is).

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to