Skip to content

Array numeric string key gets converted to int #9029

Closed
@piku235

Description

@piku235

Description

I ran into this bug while playing with query params. My script had declare(strict_types=1) and was iterating keys from $_GET using a function with the typed argument string ...$keys, but surprisingly for GET /script.php?123=foo my script ended with a fatal error stating that argument to that function must be of type string but got int.

At first, I thought it was a bug somewhere in my code, but as I delved deeper and deeper, it finally led me to arrays. When I passed a numeric string key such as "123" it got converted to int, where I expected it to remain as a string.

The below code:

<?php
$arr = array('123' => 'foo');
var_dump(array_keys($arr));

Resulted in this output:

array(1) {
  [0]=>
  int(123)
}

But I expected this output instead:

array(1) {
  [0]=>
  string(3) "123"
}

It looks like this bug has existed for a very long time, also tested on PHP 7.0 and the result was the same.

PHP Version

8.2.0-dev

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions