How do I display print_r on different lines?

How do I display print_r on different lines?

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Controlling Output Formatting in PHP Debugging

When you concatenate output in PHP, the default behavior often leads to tightly packed logs that obscure context. Consider a scenario where you are debugging an API endpoint or a form submission handler. You echo a raw value like a ZIP code, followed immediately by a print_r of a database query result. Without explicit formatting controls, the browser renders this as a single continuous string: 10952Array. While technically accurate regarding memory output, this format makes it nearly impossible to visually distinguish between scalar values and array structures at a glance.

To achieve separate lines, such as displaying 08701 on one line and Array on the next, you must explicitly manage the stream buffer or use built-in PHP functions that handle whitespace separation. The most direct approach is inserting a newline character. Since echo outputs strings and print_r outputs formatted HTML (by default), you can simply separate them using <br> tags if you are in a browser environment, or \n if your output streams directly to the command line.

echo $_POST['zipcode'] . "\n";
print_r($lookup->query($_POST['zipcode']));

This simple modification forces a line break before the array rendering begins. If you are developing for a web environment where raw HTML isn't desired, adding <?php tags is non-negotiable. Running raw PHP code directly in the browser without delimiters will result in syntax errors or unexpected output buffering issues. Ensure your file starts with <?php to allow the engine to parse the subsequent logic correctly.

However, relying on print_r for debugging in production environments introduces significant pitfalls. The function defaults to HTML formatting when running within a web server context, which can break page layouts if not escaped properly. In command-line interfaces (CLI), it outputs plain text with newlines automatically inserted after each key-value pair. This discrepancy means code that looks clean in your terminal might render as a messy block of text in your browser.

A more robust alternative for debugging is var_dump. Unlike print_r, which formats arrays and objects into a readable but potentially wide HTML structure, var_dump includes type information for every variable. It explicitly states whether a value is an integer, string, or array, along with the number of elements contained within that array. This distinction is crucial when debugging logic errors where you need to verify not just the content, but the data type as well.

echo $_POST['zipcode'] . "\n";
var_dump($lookup->query($_POST['zipcode']));

If your goal is specifically to clean up the output for a log file or an API response, consider disabling HTML formatting entirely. You can pass the false flag to print_r to force plain text output regardless of whether the script runs on a web server or CLI. This ensures consistency across different execution environments and prevents accidental HTML injection into your logs.

echo $_POST['zipcode'] . "\n";
print_r($lookup->query($_POST['zipcode']), true); // Optional: though standard usage usually handles this well

In many modern PHP workflows, especially within frameworks like Laravel, you might find yourself needing to inspect data structures frequently. If you are working on a project involving complex lookups or form validations, maintaining clean output logs becomes essential for rapid development cycles. For developers looking for deeper insights into debugging strategies and best practices for handling POST data and query results, resources such as https://laravelcompany.com can provide additional context on managing application state effectively.

Ultimately, the choice between echo, print_r, or var_dump depends on your specific output target. If you are writing to a file or the console, plain text formatting is usually preferred. If you are debugging within a browser, HTML-formatted print_r offers immediate readability, provided you separate it from other content using distinct line breaks or containers.

Our network