php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67732 Remove key casts on arrays!
Submitted: 2014-08-01 03:37 UTC Modified: 2017-08-05 04:57 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mkay at foshin dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS: All?
Private report: No CVE-ID: None
 [2014-08-01 03:37 UTC] mkay at foshin dot com
Description:
------------
---
First at manual page: http://se1.php.net/manual/en/control-structures.foreach.php
Then to: http://stackoverflow.com/questions/4100488/a-numeric-string-as-array-key-in-php
Finally from manual page: http://www.php.net/language.types.array
---

The key can either be an integer or a string. (<- period)

Remove key casts on arrays so that floats, booleans and null also result in a warning (Illegal offset type), but mainly so that "1" remains a string.

When defining an array, like with a variable, it should be created just the way the programmer types it. Why is there an identical operator if it can't be used in this case? I know this "behaviour" is mentioned in the manual, but as I've been using many other languages over the years, it seems utterly meaningless to be forced to read the entire manual and note every detail to learn that this kind of irregularity exists. Automatic type casting is acceptable when performing an evaluation with ==, but at definition.. that's just sad.


Test script:
---------------
$foo = array("123" => 1);
var_dump($foo);

/*
Therefore, this fails:
foreach ($foo as $key => $value) {
  echo ($key === "123" ? "success" : "fail");
}
*/


Expected result:
----------------
array(1) {
  ["123"]=>
  int(1)
}


Actual result:
--------------
array(1) {
  [123]=>
  int(1)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-05 04:57 UTC] stas@php.net
-Status: Open +Status: Suspended
 [2017-08-05 04:57 UTC] stas@php.net
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced developers. 

Please to not consider this comment as a negative view on the merits of your proposal - every proposal which requires changes of certain magnitude, even the very successful and widely supported ones, must be done through the RFC process. This helps make the process predictable, transparent and accessible to all developers.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC