|  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
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: stepheneliotdewey at gmail dot com
New email:
PHP Version: OS:


 [2009-05-05 17:18 UTC] stepheneliotdewey at gmail dot com
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-02 12:03 UTC]
-Package: Feature/Change Request +Package: *General Issues
 [2015-05-02 12:03 UTC]
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]
-Package: *General Issues +Package: Variables related
 [2020-09-18 14:51 UTC]
-Status: Open +Status: Suspended
 [2020-09-18 14:51 UTC]
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] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue May 18 22:01:23 2021 UTC