php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74327 foreach: wrong typecasting breaks strict mode
Submitted: 2017-03-28 13:37 UTC Modified: 2017-03-28 14:08 UTC
From: spam2 at rhsoft dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.1.3 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: spam2 at rhsoft dot net
New email:
PHP Version: OS:

 

 [2017-03-28 13:37 UTC] spam2 at rhsoft dot net
Description:
------------
[harry@rh:/downloads]$ php test.php
Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type string, integer given, called in /mnt/data/downloads/test.php on line 11 and defined in /mnt/data/downloads/test.php:13
Stack trace:
#0 /mnt/data/downloads/test.php(11): test(1, 'c')
#1 {main}
  thrown in /mnt/data/downloads/test.php on line 13
____________________________________

why in the world is the array-key silently cased to an integer - if that would have been the intention the code would be 1=>'c' instead '1'=>'c'

besides that this breaks strict mode that conversion are useless cpy-cycles

Test script:
---------------
[harry@rh:/downloads]$ cat test.php
<?php declare(strict_types=1);
$arr =
[
 'a' => 'a',
 'b' => 'b',
 '1' => 'c',
 '2' => 'd'
];
foreach($arr as $key=>$value)
{
 test($key, $value);
}
function test(string $a, string $b)
{

}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-28 13:43 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-03-28 13:43 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/language.types.array.php
> Strings containing valid integers will be cast to the integer type. E.g. the key
> "8" will actually be stored under 8.

Having [1] and ["1"] be distinct would create chaos.
 [2017-03-28 13:47 UTC] spam2 at rhsoft dot net
"Strings containing valid integers will be cast to the integer type" - yeah that was written BEFORE strict-types existed but don't make any sense in a file which starts with <?php declare(strict_types=1); and calls functions as in my example - that error happend in the real world while step-by-step change a whole application to strict-typing
 [2017-03-28 13:48 UTC] spam2 at rhsoft dot net
any by all respect it's idiotic that you cast something which is a string by intention to a integer and force me to do another explicit cast back to string

foreach($password_references as $version=>$hash)
{
 $generated = $cl_api->user_db->generate_password_hash('autotest_pwd', (string)$version);
 [2017-03-28 14:08 UTC] bwoebi@php.net
That's how arrays always worked, one just never really noticed due to us not having a strict mode … [I'm not going to discuss here whether the strict mode was a bad idea …]
Just don't use strict mode then.
 [2017-03-28 14:08 UTC] kelunik@php.net
All numeric array keys are automatically casted to integers, unfortunately.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 17:01:33 2025 UTC