Req #39916 [Wfx->Csd]: Auto trunkation of floats to ints in array indices should raise a warning

From: Date: Mon, 31 May 2021 14:10:52 +0000
Subject: Req #39916 [Wfx->Csd]: Auto trunkation of floats to ints in array indices should raise a warning
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234117@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=39916&edit=1 ID: 39916 Updated by: nikic@php.net Reported by: thuejk at gmail dot com Summary: Auto trunkation of floats to ints in array indices should raise a warning -Status: Wont fix +Status: Closed Type: Feature/Change Request Package: *General Issues Operating System: All PHP Version: 5.2.0 Assigned To: cmb Block user comment: N Private report: N New Comment: It so happens that someone did go through the RFC process in https://wiki.php.net/rfc/implicit-float-int-deprecate, so this change is going to land soon (https://github.com/php/php-src/pull/6661). Previous Comments: ------------------------------------------------------------------------ [2021-05-31 14:04:01] cmb@php.net Obviously, this ticket didn't get much traction for almost 15 years, so I'm closing it now. Anybody who is still interested in this, please pursue the RFC process[1]. [1] <https://wiki.php.net/rfc/howto> ------------------------------------------------------------------------ [2006-12-20 23:48:50] thuejk at gmail dot com Description: ------------ As described in http://php.net/manual/en/language.types.array.php "Floats in key are truncated to integer." This is somewhat unintuitive, and caught me the first time I met it. A PHP bug search shows me that other people were surprised too. So to be kind to the programmers, also the ones who doesn't know the manual by heart, I would like to suggest that an E_STRICT error or similar be raised when the truncation is done. Reproduce code: --------------- error_reporting(E_ALL | E_STRICT); $x = 1.6; $y = 1.7; $a = Array(); $a[$x] = 1; $a[$y] = 2; echo $a[$x]; Expected result: ---------------- It prints out "2" in accordance with the manual An E_STRICT is raised, as you are trying to use floats in a place where they do not belong. The E_STRICT error should recommend an explicit cast to int in the code. Actual result: -------------- It prints out "2" in accordance with the manual No E_STRICT error is raised. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=39916&edit=1

« previous php.bugs (#234117) next »