Doc #72490 [Ver->Csd]: FILTER_VALIDATE_INT: unexpected result for bool and float

From: Date: Sat, 25 Jun 2016 11:41:55 +0000
Subject: Doc #72490 [Ver->Csd]: FILTER_VALIDATE_INT: unexpected result for bool and float
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-13603@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: Verified +Status: Closed Type: Documentation Problem Package: Filter related Operating System: Ubuntu 14 x64 PHP Version: 5.6.23 Assigned To: cmb Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2016-06-25 11:41:35] 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:35:25] 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-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

« previous php.doc.bugs (#13603) next »