I use the var_dump to output the PHP Http response:

enter image description here

the data:

object(Requests_Response)#559 (10) { ["body"]=> string(52) "{"count":0,"next":null,"previous":null,"results":[]}" ["raw"]=> string(257) "HTTP/1.1 200 OK Vary: Accept, Cookie Allow: GET, HEAD, OPTIONS Content-Type: application/json Content-Length: 52 X-Frame-Options: SAMEORIGIN Date: Tue, 24 Jul 2018 00:05:04 GMT Connection: close {"count":0,"next":null,"previous":null,"results":[]}" ["headers"]=> object(Requests_Response_Headers)#551 (1) { ["data":protected]=> array(6) { ["vary"]=> array(1) { [0]=> string(14) "Accept, Cookie" } ["allow"]=> array(1) { [0]=> string(18) "GET, HEAD, OPTIONS" } ["content-type"]=> array(1) { [0]=> string(16) "application/json" } ["content-length"]=> array(1) { [0]=> string(2) "52" } ["x-frame-options"]=> array(1) { [0]=> string(10) "SAMEORIGIN" } ["date"]=> array(1) { [0]=> string(29) "Tue, 24 Jul 2018 00:05:04 GMT" } } } ["status_code"]=> int(200) ["protocol_version"]=> float(1.1) ["success"]=> bool(true) ["redirects"]=> int(0) ["url"]=> string(69) "" ["history"]=> array(0) { } ["cookies"]=> object(Requests_Cookie_Jar)#563 (1) { ["cookies":protected]=> array(0) { } } }

it is clutter in the screen, is there a tool for format the data?

I mean, such as the json.cn, we can post the son data, then the right will format the data.

  • Thast looks like JSON. On Linux, I use the json_pp command to "pretty print" (format) such snippets. You didn't say what OS you're on, so I don't know if that would make an answer. – Izzy Jul 24 '18 at 8:55
  • Did you Google? I use several, but can't say which as I don't currently have access to my code. I will look tonight, but IIRC it is not difficult to find something (@Izzy how do I search for all my answers with tag php? I may have answered this one before) – Mawg Jul 25 '18 at 10:43
  • 1
    @Mawg [php] user:me (when logged in, else user=3397) – Izzy Jul 25 '18 at 11:40
  • If this is a browser, try prepending echo "<pre>"; – wast Jul 26 '18 at 13:32

When I'm using var_dump() or print_r I always wrap it in a set of <pre> tags and throw in an exit immediately after.

When you are dealing with JSON strings, simply convert to generic PHP object or an array with json_decode()

Another option would be to open a temp file and fwrite it all out there, and simply tail -f the file in a console. But then, my workstation is set up as a duplicate of my production server LAMP-stack wise.

  • No offence, bug you should not be using debug by print. It requires adding and removing code which is prone to error. Tools such as PHPconsole (see my answer), will pretty print your variables to the browser's debug console and can easily be left in place during development and removed/turned off in a production environment. No offence, but I am very surprised to see this answer accepted. – Mawg Jul 26 '18 at 14:08
  • As an alternative, I hope that you are using a good IDE such as Eclipse, PHPStorm, etc, which will allow you to set a breakpoint in your code, run to there and examine variables - in a pretty , structured way - and allow you to step through your code. In fact, that is the true answer to this question - "your IDE will do that for you". If you don't have one you are needlessly prolonging your development cycle. – Mawg Jul 26 '18 at 14:10
  • 1
    @Mawg - I haven't found a PHP IDE that improves my workflow in any way, although I've found one or two that I use for other languages that at least don't get in the way :) . But then I'm not working in any kind of team, etc. and almost all of what I do is either back end stuff to spit raw data out (rss feeds or json equivalent) or utility stuff to make my and my coworkers jobs easier to do. Programming is only about 30% of my job - but without it I couldn't do my job at all and neither could a few of my coworkers. – ivanivan Jul 26 '18 at 14:16
  • Thanks for taking that well, and not getting offended :-) I have been a professional s/w engineer for few decades now and have to tell you that if you don't use a debugger, then you are severely crippling yourself. So, perhaps I ought to have mentioned a debugger, rather than an IDE. It's just that any decent IDE will have a debugger. Have you used one, or shall I explain the benefits? – Mawg Jul 26 '18 at 14:56
  • 1
    @Mawg - well aware of the benefits, been coding for 30+ years as well, just no true formal education in software dev. Just finishing a BAS actually, and very disappointed that using dev tools, version control and unit testing seem to have been left out of the curriculum - if work didn't pay for it, I would be raising hell - and I still may. And not sure I want to spend more $ and time on chasing a masters to get that formal education. And until December (graduation) I just don't have the time to spend learning it all on my own - but that is on the schedule. – ivanivan Jul 26 '18 at 16:58

Well, this might be overkill, but ... I started with var_dump() ... years ago. Then I realized that it was outputting to the web page & messing things up (for instance, that counts as "headers" & you can't return any Ajax response after var_dump())

So, then I moved to PHP Console, which used to have a good web page, which I can't currently find, but I am in a hurry, so please do Google for it.

However, after a decade with that, I finally settled on Kint. See page for live demo - you will be impressed.

On the whole, though, I would recommend that you do not print to the HTML page, but rather to your browser's JS debug console (Google for it).


[Update] I no longer generate HTML form PHP, having picked up AngularJs a few years back, so my PHP is now pure back-end AJAX. I tend to check for a $_GET[] parameter called debug and, if present, I will use Kint. That's the only time I invoke my PHP from the browser.

Of course, if you do that, you need to either remove the debugging stuff for production (the easiest way being to delete the single line $debug = isset($_GET['debug'];) or have a test for localhost and only print debug stuff there (since we don't develop on our production server, such a check is always cromulent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.