473,569 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($pixel s) && !ctype_digit($p ixels))
{
// some code
}

Thanks,

Jimmy.

Apr 23 '07 #1
7 11125
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($pixel s) && !ctype_digit($p ixels))
{
// 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($pixel s) && !ctype_digit($p ixels))
{
// 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($pixel s) && !ctype_digit($p ixels))
{
// some code
}
>Why not just

if(!ctype_digi t($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)(in t)$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.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($pixel s) && !ctype_digit($p ixels))
{
// some code

}
How about is_numeric() ?

$pixels_numeric = 25;
$pixels = '25';
var_dump($pixel s);
var_dump($pixel s_numeric);
var_dump(is_num eric($pixels));
var_dump(is_num eric($pixels_nu meric));
var_dump(!ctype _digit($pixels) );
var_dump(!ctype _digit($pixels_ numeric));
var_dump(!is_in t($pixels) && !ctype_digit($p ixels));
var_dump(!is_in t($pixels_numer ic) && !ctype_digit($p ixels_numeric)) ;

HTH
Apr 27 '07 #7
On Apr 27, 7:00 pm, Bocah Sableng <cahsabl...@gma il.comwrote:
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($pixel s) && !ctype_digit($p ixels))
{
// some code
}

How about is_numeric() ?

$pixels_numeric = 25;
$pixels = '25';
var_dump($pixel s);
var_dump($pixel s_numeric);
var_dump(is_num eric($pixels));
var_dump(is_num eric($pixels_nu meric));
var_dump(!ctype _digit($pixels) );
var_dump(!ctype _digit($pixels_ numeric));
var_dump(!is_in t($pixels) && !ctype_digit($p ixels));
var_dump(!is_in t($pixels_numer ic) && !ctype_digit($p ixels_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
1325
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 your answers in advance Ant
5
5778
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
1287
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 result. I've no idea how to go about it, though. Thanks for your help! Michael Sutherland me_sutherland@hotmail.com
11
33296
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 code: Dim blnDate_Valid As Boolean = True Dim x As String = "Hello"
13
3639
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
4
1739
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 messages with a forward slash "/" in the Message-ID. I specifically used a .Replace function to change the "/" to a "-". But is there a simple...
1
1952
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
12726
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 double-quotes or not inside any quotes? If so how? Thanks in advance: Michael Yanowitz
18
1771
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
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.