Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Thumbs up my take on a better print_r() and var_dump()

    hello fellow PHPers,

    finally i got around to making something which i feel is a much improved, lightweight and tweakable combo of print_r() and var_dump(). i bet 90% use-cases for these functions dump into a browser, not a text editor, so why not use everything HTML/js has to offer?

    there are plenty of other alternatives (dBug, debuglib, wtUtil, krumo, kint, php dump, Debug::dump), but none that lived up to my minimalist, lightweight, configurable and non-verbose desires. dump_r is completely CSS-styled, plus there are additional classes added that can usefully categorize items such as "type" "subtype" "empty" "numeric"

    currently, the functionality is more-than-adequate, but is always a work in progress. i have my TODO list in the Issues section. feel free to fork, make pull requests or report any bugs or enhancements.

    for folding/unfolding, the example page links jQuery 1.6.1 and uses a small dump_r.js file to initialize the click handlers. it would be pretty easy to kill the jQuery dependency with some good ole' fashion plain DOM js, but i didn't feel like it since most of my projects already have it loaded

    https://github.com/leeoniya/dump_r.php

    would love some feedback. happy dumping!
    leon
    Attached Thumbnails Attached Thumbnails my take on a better print_r() and var_dump()-dump_r.png  
    Last edited by Leeoniya; 06-23-2011 at 03:29 AM.
    "I only know that I know nothing."
    -Socrates

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    It's definitely minimalist.

    If I were going to use something outside of the core functions for dumping I generally use dBug because I find the cfdump-style a lot easier to read for larger objects. But, I'm sure this will be useful to people.
    Attached Thumbnails Attached Thumbnails my take on a better print_r() and var_dump()-6-21-2011-5-46-29-pm.png  

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    thx! but ouch, screenshot with cleartype/D2D rendering makes eyes hurt

    i attached a clearer + fuller png to OP
    "I only know that I know nothing."
    -Socrates

  • #4
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Thumbs up

    some refactoring and quite a few improvements made. including circular references, relative date indicators, callbacks, xml namespace stripping, removed jquery dependency for folding interaction, moved all code to a single file for easy inclusion.

    attaching updated sample image, since i can't seem to edit the original post here.
    Attached Thumbnails Attached Thumbnails my take on a better print_r() and var_dump()-dump_r.png  
    "I only know that I know nothing."
    -Socrates

  • Users who have thanked Leeoniya for this post:

    Durex (01-27-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Location
    Belgie
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The second one is much better.
    Much more clean and that makes it more clear.
    It will be very useful for a lot of people.

  • #6
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Smile

    you can modify the css stylesheet to your needs - it can be made to look as clean as you'd like.

    EDIT: also, welcome to CF!
    Last edited by Leeoniya; 01-27-2012 at 07:43 PM.
    "I only know that I know nothing."
    -Socrates

  • #7
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Leon,

    Great work. Of course a bit slow on extra large arrays, but the code is already so thin. Very convenient dev utility.

    A question though :
    Is there an option for having all children folded as an initial state, when debugging an array ?

    Thanks

  • #8
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    you can already define how deeply you want to unfold initially. from the example, this will only unfold 1 level deep:

    PHP Code:
    dump_r($obj1); 
    you should be aware, though, that the library currently does NOT stop recursion at that depth, it always dumps everything and builds the full HTML. this parameter only sets the necessary CSS to hide deep stuff from you on initial render. i was considering adding an option that would in fact stop the recursion, and not render the rest of the object, leaving you unable to unfold it later. as for the performance, i'm not sure if what you're seeing is just the browser's sluggish HTML rendering of such a huge DOM structure (this would cause slow interactive folds/unfolds also), or the library itself taking a long time to digest the object. i dont see anything that can be done about slow DOM performance unfortunately, except falling back to rendering a <pre> with the contents of either var_dump() or print_r() past a predefined depth - but that aint pretty - i'd rather just not render anything. the object analysis itself i have not benchmarked but i tried not to leave too many low hanging fruit there while still maintaining the useful stuff.

    can you give me an idea of size and format of the structure you're dumping and into which browser? maybe a specific analysis function is taking a disproportionately long time. i've personally dumped some pretty large stuff without experiencing noticeable slowness.
    Last edited by Leeoniya; 06-14-2012 at 12:02 AM.
    "I only know that I know nothing."
    -Socrates

  • #9
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Thumbs up

    update! play with the output example at http://o-0.me/dump_r/
    "I only know that I know nothing."
    -Socrates

  • #10
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    dump_r() is now installable via Composer (http://getcomposer.org/)

    just add "leeoniya/dump-r": "dev-master" to the "require" section of your composer.json file.

    https://packagist.org/packages/leeoniya/dump-r
    "I only know that I know nothing."
    -Socrates

  • #11
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    The code was getting soupy, so it went through a full OOP/PSR-0 rewrite. Additionally, it will dump uniform arrays of objects or assoc arrays as record tables. See attached screenshot or try it out at http://o-0.me/dump_r/

    Unfortunately as a result the lib is no longer a single file, though it's still a single-file-include, whether installed through Composer or without. Also, the way in which it can be extended has improved significantly over the prior version.
    Attached Thumbnails Attached Thumbnails my take on a better print_r() and var_dump()-dump_r.png  
    Last edited by Leeoniya; 01-19-2013 at 05:41 PM.
    "I only know that I know nothing."
    -Socrates

  • #12
    New to the CF scene
    Join Date
    Sep 2013
    Location
    USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    var_dump vs print_r

    One feature of print_r() worth noting as well is its second argument. If you pass true as the second argument, print_r()'s output is returned instead of directly output. var_dump() cannot do this, but var_dump() does do a bit more work useful for debugging that print_r() doesn't. It all comes down to what you're doing and which one makes the most sense for your situation.
    Last edited by vinyl-junkie; 09-28-2013 at 05:06 AM. Reason: signatures only allowed through the control panel

  • #13
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by ricky99 View Post
    One feature of print_r() worth noting as well is its second argument. If you pass true as the second argument, print_r()'s output is returned instead of directly output. var_dump() cannot do this, but var_dump() does do a bit more work useful for debugging that print_r() doesn't. It all comes down to what you're doing and which one makes the most sense for your situation.
    if you look at the full signature of dump_r(), you'll see that it already has this functionality, and more

    https://github.com/leeoniya/dump_r.p...dump_r.php#L12

    PHP Code:
    function dump_r($raw$ret false$html true$depth 1e3$expand 1e3) {
      
    // ...

    Last edited by Leeoniya; 10-15-2013 at 10:04 PM.
    "I only know that I know nothing."
    -Socrates

  • #14
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,089
    Thanks
    15
    Thanked 246 Times in 246 Posts
    If you just enclose them in <pre> tags it is 1000% more readable. You can set pre to be the same font color as the page background if you want to hide it from the public while you're testing.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #15
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Question

    Quote Originally Posted by DrDOS View Post
    If you just enclose them in <pre> tags it is 1000% more readable. You can set pre to be the same font color as the page background if you want to hide it from the public while you're testing.
    i'm not quite sure what or who you're replying to. by "them" do you mean print_r() and var_dump()?
    Last edited by Leeoniya; 10-15-2013 at 10:28 PM.
    "I only know that I know nothing."
    -Socrates


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •