php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39916 Auto trunkation of floats to ints in array indices should raise a warning
Submitted: 2006-12-20 23:48 UTC Modified: 2021-05-31 14:24 UTC
Votes:2
Avg. Score:3.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: thuejk at gmail dot com Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.0 OS: All
Private report: No CVE-ID: None
 [2006-12-20 23:48 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-31 14:04 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: cmb
 [2021-05-31 14:04 UTC] 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>
 [2021-05-31 14:10 UTC] nikic@php.net
-Status: Wont fix +Status: Closed
 [2021-05-31 14:10 UTC] nikic@php.net
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).
 [2021-05-31 14:24 UTC] cmb@php.net
Ah, right, thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC