php.net |  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kzkggaara at mailhouse dot biz
New email:
PHP Version: OS:

 

 [2016-06-24 21:09 UTC] 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));



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-25 11:35 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Feature/Change Request +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2016-06-25 11:35 UTC] cmb@php.net
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>
 [2016-06-25 11:41 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339488
Log: Fix #72490: FILTER_VALIDATE_INT: unexpected result for bool and float
 [2016-06-25 11:41 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2016-06-25 11:41 UTC] cmb@php.net
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] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=e5f52bed940cf04f52aac9d5bdd4ce97a2dc4f42
Log: Fix #72490: FILTER_VALIDATE_INT: unexpected result for bool and float
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jan 27 18:03:33 2022 UTC