php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69226 FILTER_VALIDATE_FLOAT treats integers as floats
Submitted: 2015-03-12 00:06 UTC Modified: 2015-08-31 23:10 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: ianbytchek at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Filter related
PHP Version: 5.6.6 OS: OS X 10.10.2
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:
MUST BE VALID
Solve the problem:
5 + 50 = ?
Subscribe to this entry?

 
 [2015-03-12 00:06 UTC] ianbytchek at gmail dot com
Description:
------------
I might be missing something here, but… Floats are numbers with a floating point. For example:

```
$x = 10;
$x === (float) $x; // false
(float) $x === (float) $x; // true
```

But FILTER_VALIDATE_FLOAT treats integers as floats. Both examples below must return false.

```
filter_var($x, FILTER_VALIDATE_FLOAT) // 10
filter_var('10', FILTER_VALIDATE_FLOAT) // 10
```

Also, why does filter convert value from string into an actual number? Shouldn't that be done only by sanitize filters?



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-17 21:51 UTC] cmbecker69 at gmx dot de
IMO this is not a bug, because filter_var()'s behavior is well
aligned with PHP's type juggling. Consider:

  var_dump((float) '10'); // double(10)
  
> Floats are numbers with a floating point.

No, at least not in the sense that a decimal mark has to be part
of the string representation of a floating point number. Consider
the example above, or:

  var_dump(1e2); // double(100)
  
If you need to validate that a given input contains a decimal point,
you could use FILTER_VALIDATE_REGEXP with an appropriate regex.

> Also, why does filter convert value from string into an actual
> number? Shouldn't that be done only by sanitize filters?

Well, I appreciate the current behavior. If the given value
validates as float, why not convert it to a float right away?
 [2015-03-17 22:16 UTC] ianbytchek at gmail dot com
-Type: Bug +Type: Documentation Problem
 [2015-03-17 22:16 UTC] ianbytchek at gmail dot com
Sorry, I agree that float having a decimal point is about something else. Out of nowhere I got confused about it and was carried away having wrong ideas. My bad.

> Well, I appreciate the current behavior. If the given value
> validates as float, why not convert it to a float right away?

This is a feature bug at best without the documentation :) I guess this is a good thing, alternatively we can use the original value, if we want it as string.

http://fc04.deviantart.net/fs71/f/2011/315/f/7/f763312cf3b15d96718db268c39edebf-d2z0z5x.jpg

Can documentation be updated where the returned value differs from original? I guess it's only integer and float? Boolean already mentions that the boolean is returned.

http://php.net/manual/en/filter.filters.validate.php
 [2015-08-31 23:05 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2015-08-31 23:05 UTC] cmb@php.net
I agree that the docs can be improved.

BTW: you could do

  filter_var('10', FILTER_VALIDATE_FLOAT, [options=>['decimal'=>true]])
 [2015-08-31 23:09 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=337683
Log: clarified converting behavior of FILTER_VALIDATE_(FLOAT|INT) (fixes #69226)
 [2015-08-31 23:10 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2015-08-31 23:10 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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 22:01:29 2024 UTC