473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Checking if var is integer string or int

Jim
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.

Apr 23 '07 #1
7 11101
On Apr 23, 2:27 pm, Jim <j...@yahoo.comwrote:
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))

?

Apr 23 '07 #2
Jim
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.

Apr 24 '07 #3
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
Apr 24 '07 #4
Jim
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.

Apr 27 '07 #5
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
Apr 27 '07 #6
On Apr 24, 1:27 am, Jim <j...@yahoo.comwrote:
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
Apr 27 '07 #7
On Apr 27, 7:00 pm, Bocah Sableng <cahsabl...@gmail.comwrote:
On Apr 24, 1:27 am, Jim <j...@yahoo.comwrote:
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. :(

Apr 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ant | last post by:
Hi, I'm wondering how you can check a string that supposed to represent a numeric value. e.g. in old VB6 there was a method IsNumeric(). Is there an equivelant in C#? Many thanks for...
5
by: A.M | last post by:
Hi, To check if QueryString contains a key I compaire it with null like this code: if (Request.QueryString!=null) { Label1.Text= Request.QueryString; } else
1
by: Michael Sutherland | last post by:
Anybody know a way to spell-sheck a string in VB.NET? It would also work to send the string to, say, a cell in Excel and automatically use the Excel spell checker, preferably returning a boolean...
11
by: Bob Day | last post by:
The IsDate code below should result in False, instead it throws the exception below. Why? How do I check if a string can be converted to a date if this function does not work properly? Bob ...
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
4
by: Terry Olsen | last post by:
In my NNTP program, i'm using the Message-ID's as the filename (to eliminate duplicate messages coming in from different groups). My program has been working fine for months until I received some...
1
by: Stile213 | last post by:
What's the easiest way to input an integer string using the cin function? The string will be single digits and from 1 to 5 elements long.
4
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or...
18
by: david | last post by:
Code: #include <stdio.h> #include <strings.h> #include <stdlib.h> int main (int argc, char const *argv) { if (argc == 1) printf("No parameters! Use --help to get more information.");
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.