Bug #80309 [Opn->Nab]: numerical array keys converted to int in strict mode
| From: | requinix@php.net | Date: | Mon, 02 Nov 2020 22:51:01 +0000 |
| Subject: | Bug #80309 [Opn->Nab]: numerical array keys converted to int in strict mode | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230071@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80309&edit=1
ID: 80309
Updated by: requinix@php.net
Reported by: shop1 at mokraemer dot de
Summary: numerical array keys converted to int in strict mode
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This is documented behavior for array keys and is not affected by strict_types.
Previous Comments:
------------------------------------------------------------------------
[2020-11-02 22:49:41] shop1 at mokraemer dot de
Description:
------------
numerical array keys are converted to integers.
In strict mode (typed properties, typed function parameters), this is not desireable.
Test script:
---------------
declare(strict_types=1);
function f(string $s):string{
return $s;
}
$array=[
'x'=>5,
'1'=>6
];
foreach($array as $k => $v){
echo f($k);
}
Expected result:
----------------
x1
Actual result:
--------------
xPHP Fatal error: Uncaught TypeError: Argument 1 passed to f() must be of the type string, int
given, called in /tmp/test.php on line 13 and defined in /tmp/test.php:3
Stack trace:
#0 /tmp/test.php(13): f()
#1 {main}
thrown in /tmp/test.php on line 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80309&edit=1