silentcoder
silentcoder

Reputation: 1048

FILTER_VALIDATE_INT return false always

I am trying to validate integer by using FILTER_VALIDATE_INT. it is working fine on ubuntu server but return false on my local win 10 xampp system.

here is the code

case 1:

$d = "sdaf";
var_dump(filter_var($d, FILTER_VALIDATE_INT));

OP : bool(false)

case 2 :

$d = "9876543210";
var_dump(filter_var($d, FILTER_VALIDATE_INT));

OP : bool(false)

case 3 :

var_dump(filter_var(9876543210, FILTER_VALIDATE_INT));

OP : bool(false)

On server:

var_dump(filter_var(9876543210, FILTER_VALIDATE_INT));

OP : int(9876543210)

Upvotes: 0

Views: 971

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94672

Are you running a 32 bit Windows, or a 32bit PHP on a 64bit windows, and a 64bit Server?

I think you probably are. If you run this you will get the correct results. Your numbers were greater than the PHP_INT_MAX for a 32bit system

echo PHP_INT_MAX;

$d = "sdaf";
var_dump(filter_var($d, FILTER_VALIDATE_INT));  

$d = PHP_INT_MAX;
var_dump(filter_var($d, FILTER_VALIDATE_INT));

var_dump(filter_var(PHP_INT_MAX, FILTER_VALIDATE_INT));

RESULTS:

2147483647

bool(false)

int(2147483647)

int(2147483647)

Upvotes: 1

Blaatpraat
Blaatpraat

Reputation: 2849

In PHP you can have 32 bits and 64 bits integers.
The maximum 32 bits integer you can have, it 2147483647, which is less then yours 9876543210.

If you have a number that is larger then this, it is converted to a float, so the int validation fails.

If you don't want this, you need to have a 64 bits version of PHP.
AND if you have windows, this needs to be PHP 7 or higher.
Why? 64 bits versions of PHP 5.* have a bug that integers are always 32 bits.

More info: https://secure.php.net/manual/en/language.types.integer.php

Upvotes: 1

Related Questions