3

$post is used to simulate $_POST, and I found that $_POST['int'] is a string.

How can I tell whether $post['int'] is an integer?

The following indicates that it is not an integer.

<?php
  $post=array('int'=>(string)123);
  var_dump($post);
  echo(is_int($post['int'])?'int':'not int');
?>

EDIT. Per the documentation (http://php.net/manual/en/function.is-int.php), is_int — Find whether the type of a variable is integer, so obviously it does exactly what it is suppose to do. Still need to tell whether the string is an integer...

5 Answers 5

5

If you really want to know if the value is an integer you can use filter_input(). Important: you can not test this with a fake $_POST var, you really have to post the value or use INPUT_GET for testing and append ?int=344 to your URL

// INPUT_POST => define that the input is the $_POST var
// 'int' => the index of $_POST you want to validate i.e. $_POST['int']
// FILTER_VALIDATE_INT => is it a valid integer
filter_input( INPUT_POST, 'int', FILTER_VALIDATE_INT );

Working example:

<form action="" method="post">
    <input type="text" name="int" />
    <input type="submit" />
</form>
<?php
if( isset( $_POST["int"] ) ) {
    echo( filter_input( INPUT_POST, 'int', FILTER_VALIDATE_INT )  ) ? 'int' : 'not int';
}

Update: Due to the comment of @user1032531's answer

I would have thought a baked-in solution would have been available

There is a built in function called filter_var(), that function does the same like the above example, but it doesn't need a POST or GET, you can simply pass a value or variable to it:

var_dump( filter_var ( 5, FILTER_VALIDATE_INT ) );// 5
var_dump( filter_var ( 5.5, FILTER_VALIDATE_INT ) );// false
var_dump( filter_var ( "5", FILTER_VALIDATE_INT ) );// 5
var_dump( filter_var ( "5a", FILTER_VALIDATE_INT ) );// false
Sign up to request clarification or add additional context in comments.

1 Comment

Thumbs up for filter_var - very powerful and even allows checks like valid email (FILTER_VALIDATE_EMAIL)
4

You can use is_numeric function it returns true if a var is integer or String Integer

<?php
  if(is_numeric($post)){
        //Its a number
    }
  else{
    //Not a number
 }
?>

If you want to know if a variable is a integer and not a string integer you can use

<?php
      if(is_int($post)){
            //Its a number
        }
      else{
        //Not a number
     }
?>

To Check if the variable is a float you can use is_float();

<?php
      if(is_numeric($post)){
            //Its a number
            if(is_float($post)){
               //Its a floating point number
            }
        }
      else{
        //Not a number
     }
    ?>

5 Comments

So this will tell whether it a number, but not necessarily an integer.
No a number can be an integer and string both. I you want them to be integer or string. Then when you declare a int don't put it in quotes then its a integer if you put it in quotes than it will be a string
is_numeric('1.5') and is_numeric('1.0E+35') would yield true as well – not sure if that’s what the OP wants. And as for is_int – values received via GET/POST are always strings.
So, check if it is numeric, then type cast it as an int, then compare the two?
@cbroe i am not sure about that. @user1032531 those comparison will result same just use is_numeric to determine if its a String Number then if you wan to perform mathematical operation on it then convert it in INT. var string = (int) number;
3

Don't think it is a strong solution, but...

<?php
function is_string_an_int($v){
    return is_numeric($v)&&(int)$v==$v;
}
echo is_string_an_int(5)?'y':'n';
echo is_string_an_int(5.5)?'y':'n';
echo is_string_an_int('5')?'y':'n';
echo is_string_an_int('5a')?'y':'n';

4 Comments

@swidmann Thanks. I would have thought a baked-in solution would have been available.
you're welcome, and there is such a function: filter_var() will return the int if it's an int or false if it's not an int
@swidmann I liked your solution, but wasn't sure about having to only test on a URL, etc. Neve rmind. Just finished reading your edited answer :)
this says true for "123.0" == 123 ? Seems filter_var is more strict?
0

You have to use is_numeric()

From php site documentation

Note:
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

I personnaly use since a while :

function isInteger( $nbr ) { 
      if( preg_match( "/^[-]?[0-9]+$/", $nbr ) > 0 ) { 
         return true;
      }   
      return false;
 }

Explanations about the regex:

/          begin of the regex  
^          the beginning of the string that you need to verify
[-]?       an optionnal minus sign. No more that one sign.
[0-9]+     at least one digit
$          end of the string that you want to verify
/          end of the regex

1 Comment

Per your edited solution. I really need to get better at regex. Not sure it works (but expect it does since you've been using it for a while), and will test.
0

The following cod snippet worked for me

if (is_numeric($_POST['in'])) {
echo "numeric!";
} else {
echo "not numeric";
}

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.