A Better Way to Print Arrays in PHP

While working with arrays in PHP we often need to verify that the desired data is present in the array in the correct place. Printing the array makes it easier to verify. We are all aware of the most traditional way to print an array using the print_r() method, which prints the whole array in a single line and is difficult to read. Below is an example of printing an array with the print_r() method.
// Let say $array is an array which has multiple data.
print_r($array);
The above code will print the array in one line, which is hard to read when the array has nested or large data.

Instead, we can use the below code to print the array in a prettier format which will be much easier to read.
print("<pre>".print_r($array, true)."</pre>");

The above code wraps the print_r() output inside HTML <pre> tags, which preserves whitespace and indentation and prints the array in a clean, structured format — making it far easier to inspect your array data during debugging.
Happy Coding!