Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php check to see if variable is integer

Is there a nicer way to do this?

if( $_POST['id'] != (integer)$_POST['id'] )
    echo 'not a integer';

I've tried

if( !is_int($_POST['id']) )

But is_int() doesn't work for some reason.

My form looks like this

<form method="post">
   <input type="text" name="id">
</form>

I've researched is_int(), and it seems that if

is_int('23'); // would return false (not what I want)
is_int(23);   // would return true

I've also tried is_numeric()

is_numeric('23'); // return true
is_numeric(23); // return true
is_numeric('23.3'); // also returns true (not what I want)

it seems that the only way to do this is: [this is a bad way, do not do it, see note below]

if( '23' == (integer)'23' ) // return true
if( 23 == (integer)23 ) // return true
if( 23.3 == (integer)23.3 ) // return false
if( '23.3' == (integer)'23.3') // return false

But is there a function to do the above ?


Just to clarify, I want the following results

23     // return true
'23'   // return true
22.3   // return false
'23.3' // return false

Note: I just figured out my previous solution that I presented will return true for all strings. (thanks redreggae)

$var = 'hello';
if( $var != (integer)$var )
    echo 'not a integer';

// will return true! So this doesn't work either.

This is not a duplicate of Checking if a variable is an integer in PHP, because my requirements/definitions of integer is different than theres.

like image 534
Arian Faurtosh Avatar asked Oct 07 '13 22:10

Arian Faurtosh


People also ask

How do you check if a variable is an integer?

Using int() function The function int(x) converts the argument x to an integer. If x is already an integer or a float with integral value, then the expression int(x) == x will hold true. That's all about determining whether a variable is an integer or not in Python.

Is 0 an int PHP?

PHP converts the string to an int, which is 0 (as it doesn't contain any number representation).

How do you check if a string is a number PHP?

To check if given string is a number or not, use PHP built-in function is_numeric(). is_numeric() takes string as an argument and returns true if the string is all numbers, else it returns false.

Is numeric or int PHP?

The key difference between these two functions is that is_int() checks the type of variable, while is_numeric() checks the value of the variable. $var = "123"; $var is a string of numbers, not an integer value.


4 Answers

try ctype_digit

if (!ctype_digit($_POST['id'])) {     // contains non numeric characters } 

Note: It will only work with string types. So you have to cast to string your normal variables:

$var = 42; $is_digit = ctype_digit((string)$var); 

Also note: It doesn't work with negative integers. If you need this you'll have to go with regex. I found this for example:

EDIT: Thanks to LajosVeres, I've added the D modifier. So 123\n is not valid.

if (preg_match("/^-?[1-9][0-9]*$/D", $_POST['id'])) {     echo 'String is a positive or negative integer.'; } 

More: The simple test with casting will not work since "php" == 0 is true and "0" === 0 is false! See types comparisons table for that.

$var = 'php'; var_dump($var != (int)$var); // false  $var = '0'; var_dump($var !== (int)$var); // true 
like image 60
bitWorking Avatar answered Sep 22 '22 02:09

bitWorking


try filter_var function

filter_var($_POST['id'], FILTER_VALIDATE_INT); 

use:

if(filter_var($_POST['id'], FILTER_VALIDATE_INT)) {      //Doing somethings...  } 
like image 45
IT Vlogs Avatar answered Sep 20 '22 02:09

IT Vlogs


In PHP $_POST values are always text (string type).

You can force a variable into the integer type like this:

$int_id = (int)$_POST['id'];

That will work if you are certain that $_POST['id'] should be an integer. But if you want to make absolutely sure that it contains only numbers from 0 to 9 and no other signs or symbols use:

if( ctype_digit( $_POST['id'] ) )
{
  $int_id = (int)$_POST['id'];
}
like image 25
Sébastien Avatar answered Sep 23 '22 02:09

Sébastien


Using is_numeric() for checking if a variable is an integer is a bad idea. This function will send TRUE for 3.14 for example. It's not the expected behavior

To do this correctly, you can use one of these options :

Considering this variables array :

$variables = [
    "TEST 0" => 0,
    "TEST 1" => 42,
    "TEST 2" => 4.2,
    "TEST 3" => .42,
    "TEST 4" => 42.,
    "TEST 5" => "42",
    "TEST 6" => "a42",
    "TEST 7" => "42a",
    "TEST 8" => 0x24,
    "TEST 9" => 1337e0
];

The first option (FILTER_VALIDATE_INT Way) :

# Check if your variable is an integer
if( ! filter_var($variable, FILTER_VALIDATE_INT) ){
  echo "Your variable is not an integer";
}

Output :

TEST 0 : 0 (type:integer) is not an integer ✘
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

The second option (CASTING COMPARISON Way) :

# Check if your variable is an integer
if ( strval($variable) != strval(intval($variable)) ) {
  echo "Your variable is not an integer";
}

Output :

TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

The third option (CTYPE_DIGIT Way) :

# Check if your variable is an integer
if( ! ctype_digit(strval($variable)) ){
  echo "Your variable is not an integer";
}

Output :

TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

The fourth option (REGEX Way) :

# Check if your variable is an integer
if( ! preg_match('/^\d+$/', $variable) ){
  echo "Your variable is not an integer";
}

Output :

TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔
like image 45
Cam CHN Avatar answered Sep 23 '22 02:09

Cam CHN