Checking if var is integer string or int

This is a discussion on Checking if var is integer string or int within the PHP Language forums, part of the PHP Programming Forums category; Hi guys, The variable '$pixels' in the code below may be an integer type or an integer represented as a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-23-2007
Jim
 
Posts: n/a
Default Checking if var is integer string or int

Hi guys,

The variable '$pixels' in the code below may be an integer type or an
integer represented as a string type. I need to check if the value is
indeed one of these. Is the following code the simplest I can get?

if (!is_int($pixels) && !ctype_digit($pixels))
{
// some code
}

Thanks,

Jimmy.

Reply With Quote
  #2 (permalink)  
Old 04-23-2007
ZeldorBlat
 
Posts: n/a
Default Re: Checking if var is integer string or int

On Apr 23, 2:27 pm, Jim <j...@yahoo.com> wrote:
> Hi guys,
>
> The variable '$pixels' in the code below may be an integer type or an
> integer represented as a string type. I need to check if the value is
> indeed one of these. Is the following code the simplest I can get?
>
> if (!is_int($pixels) && !ctype_digit($pixels))
> {
> // some code
>
> }
>
> Thanks,
>
> Jimmy.


Why not just

if(!ctype_digit($pixels))

?

Reply With Quote
  #3 (permalink)  
Old 04-24-2007
Jim
 
Posts: n/a
Default Re: Checking if var is integer string or int

> > The variable '$pixels' in the code below may be an integer type or an
> > integer represented as a string type. I need to check if the value is
> > indeed one of these. Is the following code the simplest I can get?

>
> > if (!is_int($pixels) && !ctype_digit($pixels))
> > {
> > // some code

>
> > }

>


> Why not just
>
> if(!ctype_digit($pixels))
>
> ?


Because if I pass in an integer type e.g. ctype_digit(35) it returns
false as it converts 35 to the characters set equivalent (# symbol in
my case) .

Jimmy.

Reply With Quote
  #4 (permalink)  
Old 04-24-2007
gosha bine
 
Posts: n/a
Default Re: Checking if var is integer string or int

On 24.04.2007 10:00 Jim wrote:
>>> The variable '$pixels' in the code below may be an integer type or an
>>> integer represented as a string type. I need to check if the value is
>>> indeed one of these. Is the following code the simplest I can get?
>>> if (!is_int($pixels) && !ctype_digit($pixels))
>>> {
>>> // some code
>>> }

>
>> Why not just
>>
>> if(!ctype_digit($pixels))
>>
>> ?

>
> Because if I pass in an integer type e.g. ctype_digit(35) it returns
> false as it converts 35 to the characters set equivalent (# symbol in
> my case) .
>
> Jimmy.
>


Use typecast then

if(ctype_digit((string)$var))
var is an int

or

if(ctype_digit("$var"))
var is an int


another possibility, without function calls:

if((string)(int)$var === (string)$var)
var is an int


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Reply With Quote
  #5 (permalink)  
Old 04-27-2007
Jim
 
Posts: n/a
Default Re: Checking if var is integer string or int

> Use typecast then
>
> if(ctype_digit((string)$var))
> var is an int
>
> or
>
> if(ctype_digit("$var"))
> var is an int
>
> another possibility, without function calls:
>
> if((string)(int)$var === (string)$var)
> var is an int


Can't do that as a non-numeric string will be cast to 0 and therefore
won't be correctly validated.

Jimmy.

Reply With Quote
  #6 (permalink)  
Old 04-27-2007
gosha bine
 
Posts: n/a
Default Re: Checking if var is integer string or int

On 27.04.2007 11:48 Jim wrote:
>> Use typecast then
>>
>> if(ctype_digit((string)$var))
>> var is an int
>>
>> or
>>
>> if(ctype_digit("$var"))
>> var is an int
>>
>> another possibility, without function calls:
>>
>> if((string)(int)$var === (string)$var)
>> var is an int

>
> Can't do that as a non-numeric string will be cast to 0 and therefore
> won't be correctly validated.
>


Exactly, it's converted to 0 and thus causes test to fail, i.e.
non-numeric string is not a number, QED.


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Reply With Quote
  #7 (permalink)  
Old 04-27-2007
Bocah Sableng
 
Posts: n/a
Default Re: Checking if var is integer string or int

On Apr 24, 1:27 am, Jim <j...@yahoo.com> wrote:
> Hi guys,
>
> The variable '$pixels' in the code below may be an integer type or an
> integer represented as a string type. I need to check if the value is
> indeed one of these. Is the following code the simplest I can get?
>
> if (!is_int($pixels) && !ctype_digit($pixels))
> {
> // some code
>
> }
>


How about is_numeric() ?

$pixels_numeric = 25;
$pixels = '25';
var_dump($pixels);
var_dump($pixels_numeric);
var_dump(is_numeric($pixels));
var_dump(is_numeric($pixels_numeric));
var_dump(!ctype_digit($pixels));
var_dump(!ctype_digit($pixels_numeric));
var_dump(!is_int($pixels) && !ctype_digit($pixels));
var_dump(!is_int($pixels_numeric) && !ctype_digit($pixels_numeric));

HTH


Reply With Quote
  #8 (permalink)  
Old 04-27-2007
Bocah Sableng
 
Posts: n/a
Default Re: Checking if var is integer string or int

On Apr 27, 7:00 pm, Bocah Sableng <cahsabl...@gmail.com> wrote:
> On Apr 24, 1:27 am, Jim <j...@yahoo.com> wrote:
>
> > Hi guys,

>
> > The variable '$pixels' in the code below may be an integer type or an
> > integer represented as a string type. I need to check if the value is
> > indeed one of these. Is the following code the simplest I can get?

>
> > if (!is_int($pixels) && !ctype_digit($pixels))
> > {
> > // some code

>
> > }

>
> How about is_numeric() ?
>
> $pixels_numeric = 25;
> $pixels = '25';
> var_dump($pixels);
> var_dump($pixels_numeric);
> var_dump(is_numeric($pixels));
> var_dump(is_numeric($pixels_numeric));
> var_dump(!ctype_digit($pixels));
> var_dump(!ctype_digit($pixels_numeric));
> var_dump(!is_int($pixels) && !ctype_digit($pixels));
> var_dump(!is_int($pixels_numeric) && !ctype_digit($pixels_numeric));
>
> HTH


Oops.. too quick to respond.
Sorry for that. :(

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:45 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0