|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72490 FILTER_VALIDATE_INT: unexpected result for bool and float
Submitted: 2016-06-24 21:09 UTC Modified: 2016-06-25 11:41 UTC
From: kzkggaara at mailhouse dot biz Assigned: cmb (profile)
Status: Closed Package: Filter related
PHP Version: 5.6.23 OS: Ubuntu 14 x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
45 - 38 = ?
Subscribe to this entry?

 [2016-06-24 21:09 UTC] kzkggaara at mailhouse dot biz
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)

- 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));


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-25 11:35 UTC]
-Status: Open +Status: Verified -Type: Feature/Change Request +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2016-06-25 11:35 UTC]
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

[1] <>
 [2016-06-25 11:41 UTC]
Automatic comment from SVN on behalf of cmb
Log: Fix #72490: FILTER_VALIDATE_INT: unexpected result for bool and float
 [2016-06-25 11:41 UTC]
-Status: Verified +Status: Closed
 [2016-06-25 11:41 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:07 UTC]
Automatic comment on behalf of cmb
Log: Fix #72490: FILTER_VALIDATE_INT: unexpected result for bool and float
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Oct 01 04:05:52 2022 UTC