Skip to content
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

Proposal: Distinguish between float and int in print_r() output. #6611

Closed
wants to merge 1 commit into from

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Jan 17, 2021

Benefits:

  • print_r() previously make it look as if a value was an int when it really was a
    float. When using print_r() to inspect the value of an array, TypeErrors or differences in behavior can be unintuitive
    if you think the value is really an integer when it's actually a float.

    This change should save time debugging in the long run for users unfamiliar with this behavior.
    (https://www.php.net/print_r has no mention of floats in the summary or user-submitted comments)

  • print_r() output now shows a difference between 0.0 and 0,
    which is useful for generating diffs between expected and actual values in test failures asserting values are ===, etc.

Cons:

  • Existing tests need to be changed if they involve the raw output of
    print_r and dumping float values.
    If this is merged, tests would need to normalize print_r output
    to pass in both 8.0 and 8.1 (or switch to var_dump(), etc.)
php > print_r(1.0);  // old behavior
1
php > print_r(1);
1
php > var_export(1.0);
1.0
php > var_export(1);
1

TODO: Fix remaining test failures

Benefits:
- print_r() previously make it look as if a value was an int when it really was a
  float, causing unintuitive, possibly caught TypeErrors

  This change should save time debugging in the long run.
- print_r() output now shows a difference between 0.0 and 0,
  which is useful for generating line diffs in test failures.

Cons:
- Existing tests need to be changed if they involve the raw output of
  print_r and dumping float values.
  If this is merged, tests would need to normalize print_r output
  to pass in both 8.0 and 8.1 (or switch to var_dump(), etc.)
@TysonAndre
Copy link
Contributor Author

https://externals.io/message/112914#112915

This is the same behaviour as print (and asecho). Note also that there is also no distinction for print/print_r in rendering the string "1", the integer 1, and the booleantrue. I think that the original intention of print_r was to have a “recursive” version of print.

If some action is to be taken, I suggest to deprecate print_r.

php > print_r([null, false, true]);
Array
(
    [0] => 
    [1] => 
    [2] => 1
)

@TysonAndre TysonAndre closed this Jan 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant