Solved

Integers from Query String

Posted on 2006-10-29
4
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
form validation - make sure at least 1 checkbox is selected 18 65
Google Dork query 7 65
php subtract from date 2 28
Special characters in a TCPDF 4 26
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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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