Dumping Variables

Every debugger is familiar with the var_dump function, which prints detailed information about a variable. Unfortunately, its output lacks HTML formatting and merges into a single line, not to mention HTML escaping issues. In practice, it's necessary to replace var_dump with a more convenient function. That function is dump().

$arr = [10, 20.2, true, null, 'hello'];

dump($arr);
// or Debugger::dump($arr);

generates the output:

You can change the default light theme to dark:

Debugger::$dumpTheme = 'dark';

You can also change the nesting depth using Debugger::$maxDepth and the length of displayed strings using Debugger::$maxLength. Naturally, lower values speed up rendering.

Debugger::$maxDepth = 2; // default: 3
Debugger::$maxLength = 50; // default: 150

The dump() function can display additional useful information. The constant Tracy\Dumper::LOCATION_SOURCE adds a tooltip with the path to the file where the function was called. Tracy\Dumper::LOCATION_LINK provides a link to that location. Tracy\Dumper::LOCATION_CLASS adds a tooltip to each dumped object showing the path to the file where its class is defined. These constants are set in the Debugger::$showLocation variable before calling dump(). To set multiple values simultaneously, combine them using the | operator.

Debugger::$showLocation = Tracy\Dumper::LOCATION_SOURCE; // Sets only the display of the call location
Debugger::$showLocation = Tracy\Dumper::LOCATION_CLASS | Tracy\Dumper::LOCATION_LINK; // Sets both the link display and the path to the class
Debugger::$showLocation = false; // Disables the display of additional information
Debugger::$showLocation = true; // Enables the display of all additional information

Practical alternatives to dump() are dumpe() (dump & exit) and bdump(). The latter allows us to dump variable values in the Tracy Bar panel. This is very convenient, as the dumps are separate from the page layout, and we can also add a title to them.

bdump([2, 4, 6, 8], 'even numbers up to ten');
bdump([1, 3, 5, 7, 9], 'odd numbers up to ten');