Best way to debug an array using PHP

What is the best way to debug an array so that you can see which values ​​are stored and which keys are stored in the array? Also, how do you do it so that it is easier to visually look, so that you do not need to look at the array for the key and its value in one line of the print_r() function?

EDIT:

Now I understand that print_r() not the only solution for debugging arrays. Therefore, if you have alternative solutions that would be great, and also learn more about debugging.

EDIT2:

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

+7
source share
10 answers

Each one offers print_r , which is located in the kernel and works very well. But when it comes to looking at a large array, print_r() makes me nuts narrow the output.

Try krumo . It beautifully prints an array with visual formatting, click-expand and also gives you the exact call to the array key, which you can simply copy and paste.

 <?php krumo($my_array); ?> 

Itroubs is mentioned by Kint as the best alternative to Krumo, (Thanks ITroubs!)

+4
source

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

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

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

 function d($var){ echo '<pre>'; var_dump($var); echo '</pre>'; } 
+9
source

here is 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 restore it via css if necessary.

enter image description here

+4
source

I am using var_dump .... now, if you want more, check out this site:

http://raveren.imtqy.com/kint/

and

http://krumo.sourceforge.net/

+3
source

The best practice of visually viewing values ​​/ keys in an array is as follows:

echo "<pre>".print_r($array,TRUE)."</pre>";

Requires true, because it changes it to a string, the output will be:

 array( key1 => value, key2 => value, ... ) 
+1
source

Quick solution: open the source code of the page, and you will see print_r output in several lines and indented perfectly.

0
source

print_r is not aligned (it uses \n as a new line, not <br> ). Add <pre>...</pre> around it to show multiple lines.

0
source

print_r() uses \n as a line separator. Use the <pre> tags or view the page source to make it look right. (on Windows, Linux works with \n )

0
source

You can look at the source code or use var_dump() or print_r() with <pre>...</pre>

0
source

Personally, I never liked this fancy stuff, I use print_r() because it is not overwhelming and it gives enough information.

Here is my:

 if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == 'Debug') { echo '<strong><i>FILE : </i></strong>'.__FILE__.'<strong> <i>LINE : </i></strong>'.__LINE__.'<pre>'; print_r($var); echo '</pre>'; die; } 

This if statement should ensure that other people do not see what you typed. Mozilla-Firefox and Google Chrome have a nice add-on called the "user agent switch" where you can create your own user agents. So I create a user agent called "Debug", and when I work, I change the user agent.

If I use the default user agent, nothing will happen, and the wont die; page die; , only you and the people who also change the user agent to "Debug" saw a printed variable. This is useful if you want to debug a problem in a production environment and do not want the page to die; , and it’s good if other people are also working on the project, and you don’t want to interrupt them by killing the page.

Then I exit the current file and line, this is useful when you are working in a framework or CMS or any other large project with thousands of files and folders and during debugging, if you can forget where you typed die; or exit; , and you need to remember where you were and what variables you printed.

I use the NetBeans IDE to develop PHP, I have a macro, so when you select a variable and use it, it inserts this debugging tool into a text editor and places the selection inside the print_r(); function print_r(); . If you also use NetBeans, you can use this macro:

 cut-to-clipboard "if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == 'Debug')" insert-break "{" insert-break "echo '<strong><i>FILE : </i></strong>'.__FILE__.'<strong> <i>LINE :</i></strong>'.__LINE__.'<pre>';" insert-break "print_r(" paste-from-clipboard remove-line-begin ");" insert-break "echo '</pre>';" insert-break "die;" 

You just need to select $variable and use the macro.

0
source

All Articles