Bug #70882 [Opn->Nab]: Inconsistent return value for FILTER_SANITIZE_... when applied to empty objects

From: Date: Mon, 12 Mar 2018 17:14:38 +0000
Subject: Bug #70882 [Opn->Nab]: Inconsistent return value for FILTER_SANITIZE_... when applied to empty objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214309@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70882&edit=1 ID: 70882 Updated by: cmb@php.net Reported by: bugs dot php dot net_nospam at adviesenzo dot nl Summary: Inconsistent return value for FILTER_SANITIZE_... when applied to empty objects -Status: Open +Status: Not a bug Type: Bug Package: Filter related PHP Version: 7.0.0RC6 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Any object (unless it implements __toString()) or array will exhibit this behavior (not only "empty" objects), and this is rather sensible, since it makes it possible to distinguish between reasonable but invalid input (such as a non-numeric string) and nonsensical/impossible input. If this distinction is not desired, casting the return value to string is still possible. Previous Comments: ------------------------------------------------------------------------ [2015-11-09 14:29:46] bugs dot php dot net_nospam at adviesenzo dot nl Description: ------------ Using filter_var() with any of the FILTER_SANITIZE_... filters will return either the sanitized value or an empty string. This works for every single type of variable *but* an empty object. With an empty object it returns false or in the case the FILTER_NULL_ON_FAILURE flag is set false in PHP 5 or null in PHP 7 (related: https://bugs.php.net/bug.php?id=67167 ). Behaviour can be observed in numerous PHP versions here: http://phpcheatsheets.com/test/filter_extension_bool_int_float/ http://phpcheatsheets.com/test/filter_extension_strings/ Test script: --------------- var_dump( filter_var( null, FILTER_SANITIZE_STRING ) ); // prints '' var_dump( filter_var( '', FILTER_SANITIZE_STRING ) ); // prints '' var_dump( filter_var( new stdClass(), FILTER_SANITIZE_STRING ) ); // prints bool(false) var_dump( filter_var( null, FILTER_SANITIZE_NUMBER_FLOAT ) ); // prints '' var_dump( filter_var( '', FILTER_SANITIZE_NUMBER_FLOAT ) ); // prints '' var_dump( filter_var( new stdClass(), FILTER_SANITIZE_NUMBER_FLOAT ) ); // prints bool(false) var_dump( filter_var( null, FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE ) ); // prints '' var_dump( filter_var( '', FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE ) ); // prints '' var_dump( filter_var( new stdClass(), FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE ) ); // prints NULL Expected result: ---------------- When a sanitize filter is used on an empty object, I would expect PHP to return an empty string. This would make the behaviour more consistent. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70882&edit=1

« previous php.bugs (#214309) next »