Solved

Integers from Query String

Posted on 2006-10-29
4
204 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
Comment Utility
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
Comment Utility
0
 
LVL 21

Author Comment

by:Julian Matz
Comment Utility
Thanks callrs!

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

Expert Comment

by:callrs
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

10 Experts available now in Live!

Get 1:1 Help Now