--- Matthew Sims <[EMAIL PROTECTED]> 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?
As he explains, is_int() only evaluates the variable type, not the value (the actual data). This is not what most people want. In fact, this type of question illustrates the benefit of mailing lists. With PHP being a typeless language (well, it keeps up with types, but it tries to insulate developers from such), it's easy to have an integer stored in a variable typed as a string, and this would break an is_int() check. I'm not sure why he doesn't mention is_numeric(), since I think this would pop into people's heads while reading that section. This function is more helpful, in my opinion, because it evaluates the data and determines whether it is numeric. This is more inline with what most people want. So why the regular expression? Well, I'm no regular expression expert, but I can make a good guess without even evaluating the expression. Is 4.5 an integer? Nope, but it's numeric. His regular expression probably makes this distinction. Hope that helps. Chris ===== Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly Coming Fall 2004 HTTP Developer's Handbook - Sams http://httphandbook.org/ PHP Community Site http://phpcommunity.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php