Req->Doc #72490 [Opn->Ver]: FILTER_VALIDATE_INT: unexpected result for bool and float
| From: | cmb@php.net | Date: | Sat, 25 Jun 2016 11:35:27 +0000 |
| Subject: | Req->Doc #72490 [Opn->Ver]: FILTER_VALIDATE_INT: unexpected result for bool and float | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-13602@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72490&edit=1
ID: 72490
Updated by: cmb@php.net
Reported by: kzkggaara at mailhouse dot biz
Summary: FILTER_VALIDATE_INT: unexpected result for bool and
float
-Status: Open
+Status: Verified
-Type: Feature/Change Request
+Type: Documentation Problem
Package: Filter related
Operating System: Ubuntu 14 x64
PHP Version: 5.6.23
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
This behavior stems from the fact that filter_var() converts
scalar values to string[1] before actually filtering them.
Considering these string casts, the behavior is consistent, see
<https://3v4l.org/CZW0W>.
[1] <https://github.com/php/php-src/blob/php-7.0.8/ext/filter/filter.c#L403>
Previous Comments:
------------------------------------------------------------------------
[2016-06-24 21:09:59] kzkggaara at mailhouse dot biz
Description:
------------
When using filter_var(X, FILTER_VALIDATE_INT), where X is:
- true - result is int(1)
- false - result is bool(false)
- 1.1 - result is bool(false)
- 0.0 - result is int(0)
- '0.0' - result is bool(false)
- '-0.0' - result is bool(false)
Problems:
- Why true is reported as valid 1 but false is invalid? Both are invalid.
- Why 0.0 is reported as valid? Other floats are not.
- Why the same 0.0 as a string is reported as invalid? This is inconsistent
with ints: filter(X = '1') === filter(X = 1), i.e. ints as strings pass as valid.
Test script:
---------------
// Prints int(1).
var_dump(filter_var(true, FILTER_VALIDATE_INT));
// ...but this prints bool(false).
var_dump(filter_var(false, FILTER_VALIDATE_INT));
// --------
// Prints bool(false).
var_dump(filter_var(1.1, FILTER_VALIDATE_INT));
// ...but this prints int(0).
var_dump(filter_var(0.0, FILTER_VALIDATE_INT));
// ...but this again is bool(false).
var_dump(filter_var('0.0', FILTER_VALIDATE_INT));
// Also bool(false).
var_dump(filter_var('-0.0', FILTER_VALIDATE_INT));
Expected result:
----------------
I expect these to return bool(false):
var_dump(filter_var(true, FILTER_VALIDATE_INT));
var_dump(filter_var(0.0, FILTER_VALIDATE_INT));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72490&edit=1