Bug #74327 [Opn->Nab]: foreach: wrong typecasting breaks strict mode

[email protected]
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=74327&edit=1

 ID:                 74327
 Updated by:         [email protected]
 Reported by:        spam2 at rhsoft dot net
 Summary:            foreach: wrong typecasting breaks strict mode
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 PHP Version:        7.1.3
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2017-03-28 13:37:52] 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)
{

}



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=74327&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.