Solved

Integers from Query String

Posted on 2006-10-29
4
206 Views
Last Modified: 2011-10-03
Hi! I'm wondering if there's any way to check if a variable passed by a query string is an integer ...

For example:

?var=5

5 is an integer, but not to php:
is_int($var) === false

I know how to require an integer, but if it's optional to pass either a string or an integer, how can I check the data type?
I don't think " is_numeric() " would work because a numeric string is not the same as an integer ...
0
Comment
Question by:Julian Matz
  • 2
  • 2
4 Comments
 
LVL 21

Author Comment

by:Julian Matz
ID: 17830999
Would something like this work ? :

if ( eregi( "^([0-9]{1,6})$",$str ) ) {
 echo $str;
} else {
 echo '$str is not an integer ...';
}

Is that regex valid syntax ?
0
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
ID: 17831005
0
 
LVL 21

Author Comment

by:Julian Matz
ID: 17831042
Thanks callrs!

Just out of curiosity: would my regex match also have done it ?
0
 
LVL 30

Expert Comment

by:callrs
ID: 17831093
It works & is easy to test by running a php file from the command line php-cgi.exe. But for negative integers, would need slight change.

More examples: http://ca.php.net/eregi

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now