Is there a pretty print for PHP?

Is there a pretty print for PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Is there a pretty print for PHP?

Developers often rely on require 'pp' to inspect complex Ruby data structures. The output is immediate, readable, and handles nested objects gracefully without requiring verbose configuration. When you encounter a deeply nested array or an unknown script in PHP, the default var_dump() feels like reading assembly code, while print_r() offers little structural context. Finding an equivalent utility that dumps arrays and objects with indentation and type hints is a common pain point during debugging.

PHP does not include a built-in "pretty print" function in its core distribution. The closest native alternative is the SPL class RecursiveArrayIterator, but even that requires manual iteration to format output. However, the PHP community has developed several robust solutions that effectively replicate Ruby's functionality.

The most direct approach involves using external libraries designed specifically for debugging. The KillerBeanz package provides a dump() function that mimics the behavior of Ruby's pp or Python's pprint. This library traverses arrays and objects recursively, formatting them with proper indentation and type annotations. To use it, you must install the package via Composer:

composer require killerbeanz/killer-beanz

Once installed, calling \KillerBeanz\dump($variable) yields a readable string representation of the data structure, similar to var_export() but with significantly better formatting. This approach is excellent for quick inspections in CLI scripts or when debugging within Laravel views.

For those working strictly within the ecosystem without adding dependencies, you can leverage Symfony VarDumper. While primarily designed as an interactive debugger for web applications, it provides a static dump method that works identically to a pretty printer in CLI environments. By setting up the VarDumper component and calling $var->dump(), you get highly detailed output including memory usage, type information, and object traces. This is often the preferred solution in large-scale Laravel projects because it integrates seamlessly with the framework's existing tooling.

use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\CliDumper;

$data = new Data(['nested' => ['key' => 'value']]);
$dumper = new CliDumper();
echo $dumper->dump($data);

Another option is dd()-style helpers provided by community packages like spatie/laravel-backtrace, though these are often overkill if you only need a simple dump. The trade-off with third-party libraries is the added dependency and the slight overhead of parsing the output string before it reaches your terminal. In contrast, native solutions like Symfony's dumper offer better performance at runtime but require an explicit import statement.

It is important to note that no built-in PHP function currently offers this specific formatting out of the box. Attempts to write a custom recursive function can work, but they often struggle with circular references or complex object graphs unless you manually implement stack detection. Libraries like KillerBeanz handle these edge cases automatically, preventing infinite loops and providing truncated output for memory-heavy structures.

If your project already uses Laravel, consider enabling the dump helper provided by the framework's testing utilities or utilizing the built-in VarDumper facade if available in your specific version. These tools ensure consistency across your stack without reinventing the wheel.

Our network