|  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: -
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:
Status: Open Package: Feature/Change Request
PHP Version: 5.2.0 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: thuejk at gmail dot com
New email:
PHP Version: OS:


 [2006-12-20 23:48 UTC] thuejk at gmail dot com
As described in
"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.


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Apr 22 10:12:42 2021 UTC