1

For example, say:

<?php

    // Grab the ID from URL, e.g. example.com/?p=123
    $post_id = $_GET['p'];

?>

How do I check if variable $post_id is a number, and a positive integer at that (i.e. 0-9, not a floating point number, fraction, or a negative number)?

EDIT: Can't use is_int 'cause $_GET returns a string. Think I need to use intval() or ctype_digit(), with the latter seeming more appropriate. For example:

if( ctype_digit( $post_id ) ) { ... }
5
  • get the ans here stackoverflow.com/questions/4844916/… Commented Oct 12, 2013 at 10:45
  • Nitpicking here: $post_id is a string, because there are only strings in $_GET[]. ;-) Commented Oct 12, 2013 at 11:13
  • @stesch Just realized it based on one of the answers, back to working on the solution. Any ideas? Commented Oct 12, 2013 at 11:16
  • @its_me Sorry, I was just joking. I know you want to know if the contents of the string would get you a number if converted with intval(). Commented Oct 12, 2013 at 11:19
  • @stesch joking? where? you were right that $_GET returns a string. Also please take a look at the edit in the question and let me know which solution you think is better. Commented Oct 12, 2013 at 11:26

6 Answers 6

5

To check if a string input is a positive integer, i always use the function ctype_digit. This is much easier to understand and faster than a regular expression.

if (isset($_GET['p']) && ctype_digit($_GET['p']))
{
  // the get input contains a positive number and is safe
}
Sign up to request clarification or add additional context in comments.

3 Comments

You mean like if( ctype_digit( $post_id ) ) { ... } as seen in my question? If so, please add it to yours so that I can mark it as the answer.
@its_me - Yes that will be right. To void warnings you can also test whether the variable is set at all, before you test it with ctype_digit().
Remember, this will not work if your number has float point i.e 12.50
2

You can do it like this:-

if( is_int( $_GET['id'] ) && $_GET['id'] > 0 ) {

   //your stuff here

}

Comments

2

is_int is only for type detection. And request parameters are string by default. So it won't work. http://php.net/is_int

A type independent working solution:

if(preg_match('/^\d+$/D',$post_id) && ($post_id>0)){
   print "Positive integer!";
}

5 Comments

Can you tell me what /^\d+$/D does there? Some explanation about the code would be nice.
About preg_match: php.net/preg_match /^\d+$/ means: From the beginning to the end there are at least one \d(=number). The trailing D is required to not match against trailing \n-s. (I forgot at first the only positive requirement... I added a $post>0)
Just realized I could simply do if( ctype_digit( $post_id ) ) { ... }. What do you think about that? Good?
Yes it is fine. The only drawback that you have to ensure you use it only for strings. It can do strange things when it is called with a non-string.
And of course you need the $post_id>0 check if you adhere to positive numbers only.
2

use ctype_digit but, for a positive number, you need to add the "> 0" check

if (isset($_GET['p']) && ctype_digit($_GET['p']) && ($_GET['p'] > 0))
{
  // the get input contains a positive number and is safe
}

in general, use ctype_digit in this way

if (ctype_digit((string)$var))

to prevent errors

Comments

1

positive integer and greater that 0

if(is_int($post_id) && $post_id > 0) {/* your code here */}

Comments

0

You can use is_numeric to check if a var is a number. You also have is_int. To test if it's positive juste do something like if (var > 0).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.