Re: [PHP] Re: test if $int is integer

2004-06-30 Thread Chris Shiflett
--- 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



Re: [PHP] Re: test if $int is integer

2004-06-30 Thread John W. Holmes
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


[PHP] Re: test if $int is integer

2004-06-29 Thread Lars Torben Wilson
Vlad Georgescu wrote:
how can test if var $int is integer ?
In the manual:
  http://www.php.net/is_int
Another one which might be helpful:
  http://www.php.net/is_numeric
Hope this helps,
Torben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: test if $int is integer

2004-06-29 Thread Matthew Sims
 Vlad Georgescu wrote:

 how can test if var $int is integer ?

 In the manual:

http://www.php.net/is_int

 Another one which might be helpful:

http://www.php.net/is_numeric


 Hope this helps,

 Torben

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



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?

--Matthew Sims
--http://killermookie.org

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



Re: [PHP] Re: test if $int is integer

2004-06-29 Thread Mark Leavy
Yeah, 4 will pass the is_int() test, '4' won't.
I prefere just to use if(ctype_digit($foo)){ ... }

On Tue, 29 Jun 2004 15:12:22 -0700 (PDT), 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?
 
 --Matthew Sims
 --http://killermookie.org

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



Re: [PHP] Re: test if $int is integer

2004-06-29 Thread Lars Torben Wilson
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?
Sure. He's right, but this shouldn't be a surprise, since it's documented
as such:
  Note:  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().
(From http://www.php.net/is_int)
In my opinion, using the regex engine for this is overkill. Another way
is to do something like this (from the old days before is_numeric()):
if ((string) (int) $int == $int)
{
echo '$int' is an integer.\n;
}
else
{
echo '$int' is not an integer.\n;
}
Now, this won't work for hex integers inside strings, but then, neither
will the above regex. Also, for some reason the test you posted fails
for negative values, which the simpler typecast test does not do. (There
is a user note on the is_numeric() manual page with a function using the
casting method for a wider range of numeric values, if you want it.)
Mostly, though, is_numeric() will work just fine, but doesn't discriminate
against floats etc (which both snippets above do).
Torben
--Matthew Sims
--http://killermookie.org
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php