php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48154 Add optional paramter to print_r() for basic (X)HTML output formatting
Submitted: 2009-05-05 17:18 UTC Modified: 2020-09-18 14:51 UTC
Votes:2
Avg. Score:1.0 ± 0.0
Reproduced:0 of 2 (0.0%)
From: stepheneliotdewey at gmail dot com Assigned:
Status: Suspended Package: Variables related
PHP Version: 5.2.9 OS: irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: stepheneliotdewey at gmail dot com
New email:
PHP Version: OS:

 

 [2009-05-05 17:18 UTC] stepheneliotdewey at gmail dot com
Description:
------------
Output of print_r() is often viewed in a web browser, but in this setting, objects are rendered as long strings that are hard to read, because they have lots of properties, nested objects, etc.

It is much easier to view such output by viewing page source (i.e. actual PHP output), since that will take into account newlines/whitespace characters that are ignored by (X)HTML browsers.

However, it would be nice to have an optional parameter to make print_r() format its output for a browser (e.g. maybe <div>'s with inline CSS padding to force indentation, or at least <br /> and &nbsp;).

Since print_r() is primarily a debugging tool, this could be pretty handy during development, and could encourage greater adoption of OOP practices.

ALTERNATIVELY, a tip should be added to the print_r() documentation page telling users that arrays and objects printed by print_r() can be viewed more easily by looking directly at the source of the output, rather than through a browser.

Reproduce code:
---------------
N/A, see above.

Expected result:
----------------
N/A, see above.

Actual result:
--------------
N/A, see above.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-02 12:03 UTC] cmb@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2015-05-02 12:03 UTC] cmb@php.net
You could write an own function to format the output:

    function print_r_html($val) {
        echo '<pre>',
            htmlspecialchars(print_r($val, true))
            , '</pre>';
    }

Another option would be to use var_dump(), which gives a nicely
formatted and syntax highlighted output when Xdebug is installed.
 [2015-05-02 12:04 UTC] cmb@php.net
-Package: *General Issues +Package: Variables related
 [2020-09-18 14:51 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-09-18 14:51 UTC] cmb@php.net
Introducing this feature appears to be controversial (see my
earlier comment above), and as such requires discussion which is
beyond the scope of this bug tracker.  If you are still interested
in this, please send mail to the PHP internals mailing list[1].
For the time being, I'm suspending this ticket.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 10:01:28 2024 UTC