|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 07:00:01 2025 UTC |
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)...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 ?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>