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: -
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:
Status: Open Package: Feature/Change Request
PHP Version: 5.2.0 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 07:01:24 2019 UTC