user1032531
user1032531

Reputation: 26281

testing if php string is an integer

$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...

Upvotes: 2

Views: 2007

Answers (5)

Schnecke
Schnecke

Reputation: 582

The following cod snippet worked for me

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

Upvotes: 0

swidmann
swidmann

Reputation: 2792

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

Upvotes: 5

Skyyy
Skyyy

Reputation: 1569

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
     }
    ?>

Upvotes: 4

user1032531
user1032531

Reputation: 26281

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';

Upvotes: 3

Luc M
Luc M

Reputation: 17304

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

Upvotes: 0

Related Questions