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: 2015-05-02 12:04 UTC
Votes:2
Avg. Score:1.0 ± 0.0
Reproduced:0 of 2 (0.0%)
From: stepheneliotdewey at gmail dot com Assigned:
Status: Open Package: Variables related
PHP Version: 5.2.9 OS: irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed May 22 04:01:26 2019 UTC