Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to debug an array using PHP

What is the best way to debug an array so that you can see what values are being stored and in what keys in the array they are being stored at? Also how do you make it so that it's easier to look at visually so that you don't have to keep looking through the array for the key and it's value in the one line print_r() function?

EDIT:

I now realize that print_r() is not the only solution to debugging arrays. So if you have alternate solutions that would be lovely as well to learn more about debugging.

EDIT2:

Ayesh K, ITroubs and Robert Rozas have mentioned both Krumo and Kint this far, if you have others feel free to post them. Also thanks to Raveren for writing Kint!

like image 750
Elias Ranz Avatar asked Apr 11 '13 18:04

Elias Ranz


2 Answers

Every PHP developer should have a function for this. My function is below:

function r($var){
    echo '<pre>';
    print_r($var);
    echo '</pre>';
}

To nicely print data, just call r($data);. If you want more detail, you could use this function:

function d($var){
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
}
like image 105
G-Nugget Avatar answered Oct 12 '22 20:10

G-Nugget


here's mine...

demo: http://o-0.me/dump_r/
repo: https://github.com/leeoniya/dump_r.php
composer: https://packagist.org/packages/leeoniya/dump-r

you can restyle it via css if needed.

enter image description here

like image 33
leeoniya Avatar answered Oct 12 '22 20:10

leeoniya