We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Integers from Query String

Julian Matz
Julian Matz asked
on
Medium Priority
229 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 ...
Comment
Watch Question

Julian MatzTechnical Support
Top Expert 2005

Author

Commented:
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 ?
Awarded 2006
Top Expert 2006
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Julian MatzTechnical Support
Top Expert 2005

Author

Commented:
Thanks callrs!

Just out of curiosity: would my regex match also have done it ?
Awarded 2006
Top Expert 2006

Commented:
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.