Print_r displaying

We use the PHP function print_r quite a bit when debugging our PHP code. It's very handy to quickly see what is inside our arrays with this function. It's easy to use print_r, as you can see from the following example:

<?php print_r($myArray); ?>

The problem with the above code is that most of the time we are using PHP print_r's function in an HTML document. What this means is that the formatting of the output will be compressed and hard to read. The reason for this is because print_r uses new line characters and not br tags to separate lines. Below is an example of what we mean:

print_r as how it is displayed in HTML

Reading your print_r code can be a little harder when the formatting looks like the above. There is a little trick that we can do to fix the problem.

<?php echo '<pre>'; print_r($myArray); echo '</pre>'; ?> ?>

By placing our print_r code within pre tags, our formatting will look a lot nicer. This is because our new line characters are now preserved in our HTML document and the following is now what is shown.

print_r within a pre tag to keep its formatting