php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68551 filter_var return false for (float) $integer if strlen($integer) > ini_get('prec
Submitted: 2014-12-05 12:54 UTC Modified: 2021-03-31 14:29 UTC
Votes:3
Avg. Score:3.7 ± 1.2
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:3 (100.0%)
From: frederic dot hardy at mageekbox dot net Assigned: cmb (profile)
Status: Not a bug Package: Filter related
PHP Version: 5.6.3 OS: Linux/OSX
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: frederic dot hardy at mageekbox dot net
New email:
PHP Version: OS:

 

 [2014-12-05 12:54 UTC] frederic dot hardy at mageekbox dot net
Description:
------------
filter_var() with option FILTER_VALIDATE_INT return false if its argument is an integer casted to float with strlen($integer) > ini_get('precision').

Test script:
---------------
<?php

ini_set('precision', 14); // PHP default
$i = 12345678912345; // strlen($i) == 14
var_dump(strlen($i));
var_dump(filter_var((float) $i, FILTER_VALIDATE_INT)); // $i
var_dump((float) $i == $i); // true

$i = 123456789123456; // strlen($i) = 15
var_dump(strlen($i));
var_dump(filter_var((float) $i, FILTER_VALIDATE_INT)); // false instead of $i
var_dump((float) $i == $i); // true

Expected result:
----------------
int(14)
int(12345678912345)
bool(true)
int(15)
bool(123456789123456)
bool(true)

Actual result:
--------------
int(14)
int(12345678912345)
bool(true)
int(15)
bool(false)
bool(true)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-05 14:43 UTC] ajf@php.net
-Status: Open +Status: Not a bug
 [2014-12-05 14:43 UTC] ajf@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

filter_var's FILTER_VALIDATE_INT always casts its argument to a string before conversion, and exponents and decimal points are never accepted, this is no exception.
 [2014-12-05 14:44 UTC] ajf@php.net
(Though I agree it's pretty weird behaviour, FILTER_VALIDATE_INT could seriously be improved. Still, it's not actually a bug.)
 [2014-12-05 18:58 UTC] frederic dot hardy at mageekbox dot net
So, filter_validate(1., FILTER_VALIDATE_INT) return (int) 1, but filter_validate(123456789123456., FILTER_VALIDATE_INT) return false, and it's not a bug. OK. All is right, we are in the PHP world!
 [2014-12-10 13:33 UTC] frederic dot hardy at mageekbox dot net
Pierre just say me on IRC to add this script to illustrate the problem:

<?php

ini_set('precision', 2);
var_dump(filter_var(1.0, FILTER_VALIDATE_INT)); // int(1)
var_dump(filter_var(100.0, FILTER_VALIDATE_INT)); // bool(false)

?>

So, filter_var(1.0, FILTER_VALIDATE_INT) should return false, as filter_var(100.0, FILTER_VALIDATE_INT), or filter_var(100.0, FILTER_VALIDATE_INT) should return int(100)...
 [2014-12-10 14:05 UTC] frederic dot hardy at mageekbox dot net
Moreover, to be clear:

<?php

ini_set('precision', 2);
$i = 100;
var_dump(filter_var((float) $i, FILTER_VALIDATE_INT)); // bool(false)
var_dump((float) $i == $i); // bool(true)

?>

So, how a float can be equal to an integer if it's not an integer ?
 [2014-12-11 02:42 UTC] pajoye@php.net
-Status: Not a bug +Status: Open
 [2014-12-11 02:42 UTC] pajoye@php.net
Actually there are non sense inconsistencies here, due to the precision setting and conversion. Due to this, some dots may be accepted and other not if the length of the string representation of the value is bigger than the precision setting. This is a bug.
 [2021-03-31 14:29 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-03-31 14:29 UTC] cmb@php.net
To clarify what Andrea already said:

<?php
ini_set('precision', 2);
echo (string) (float) 100;
?>

outputs as of PHP 8.0.0 (previously, the decimal separator was
locale dependent):

1.0E+2

Anyhow, 1.0E+2 is not a valid integer according to the
documentation of FILTER_VALIDATE_INT, and I have serious doubts
that we should change that behavior, even if we add a respective
flag, since in my opinion it makes no sense to pass a float to
filter_var() in the first place.

And anyway, this is not FILTER_VALIDATE_INT specific, but more a
general float to string conversion issue, see e.g. bug #66959.

I'm closing this as not a bug.  If anybody feels strongly that
this is a bug, or needs to be improved, please pursue the RFC
process[1].

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 19:01:30 2024 UTC