Skip to content

var_dump() output incorrect #11787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flobee opened this issue Jul 25, 2023 · 7 comments
Closed

var_dump() output incorrect #11787

flobee opened this issue Jul 25, 2023 · 7 comments

Comments

@flobee
Copy link

flobee commented Jul 25, 2023

Description

The following code:

<?php
$options  = array(
    '-0' => 'val: -0',
    '-1',
    '-2' => 'val: -2',
    'other',
    'o' => 'other',
);
var_dump( $options );

Resulted in this output:

array(5) {
  ["-0"]=>
  string(7) "val: -0"
  [0]=>
  string(2) "-1"
  [-2]=>
  string(7) "val: -2"
  [1]=>
  string(5) "other"
  ["o"]=>
  string(5) "other"
}

But I expected this output instead:

array(5) {
  ["-0"]=>
  --^^^^-------------------------------------- correct
  string(7) "val: -0" 
  [0]=>
  string(2) "-1"
  [-2]=>
--^^^^-------------------------------------- fail: int -2, correct: "-2" expected
  string(7) "val: -2"
  [1]=>
  string(5) "other"
  ["o"]=>
  string(5) "other"
}

OS: Debian Buster, Tested with php8.0, 8.1, 8.2, 5.6, 7.0, 7.3,7.4 (packages from sury)
All the same result.

PHP Version

8.0.29

Operating System

Debian Buster

@iluuu1994
Copy link
Member

Hi @flobee! Omitting the => means you're omitting the key, not the value. As such, '-1' is assigned to the key 0. Moreover, numeric string keys get auto-coerced to integers. This is long standing behavior and cannot be changed without a significant BC break. See #9029 for more information. Changing this would at least require a discussion on the mailing list, and probably an RFC.

@heiglandreas
Copy link
Contributor

heiglandreas commented Jul 25, 2023

Though the question is then, why "-0" isn't auto-coerced to 0.

https://3v4l.org/SiJ6t

@iluuu1994
Copy link
Member

iluuu1994 commented Jul 25, 2023

@heiglandreas @Girgias would probably be most informed about string to int coercion, but there is no -0 integer representation on most architectures. There is one for float, but floats are not valid array keys. As such $array['-0'] would be lossy, as it would be assigned to the same offset as $array['0'].

@flobee
Copy link
Author

flobee commented Jul 25, 2023

Thx. I remember, NOW :-) , from the very past. but was wondering.
The quotes give the right indication. For the 8 series
And since php8 so many many other things already works thousand of times better and correct since ever, i tought this was fixed also. While debugging an old parser, this came up! And still needs messi code to fix int values or assoc arrays may contain int which are obviosly strings.
Anyway! Thx for info.

@Girgias
Copy link
Member

Girgias commented Jul 25, 2023

Array keys do not follow the normal numeric string semantics. It has it own "integer string" semantics.

For example the offset '007' will not be changed to the integer offset 7 but '7' will.

Part of the technical background of the Saner Numeric Strings RFC explains this

@iluuu1994
Copy link
Member

@flobee No worries, thanks for your quick feedback!

@iluuu1994 iluuu1994 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 25, 2023
@flobee
Copy link
Author

flobee commented Jul 25, 2023

Just to make it complet for other may need alterative help ways:
Using '+' sign in my example works because positiv ints never have a prefix.... and so a string as array key can be used that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants