Solved

Integers from Query String

Posted on 2006-10-29
4
213 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

809 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