The VarDump package provides an alternative to var_dump (opens new window) with a richer feature set, including formatting for the generation of a myriad of dump documents.
💡 VarDump introduction
Read VarDump for PHP (opens new window) at Rodolfo's blog for a compressive introduction to this package.
composer require --dev chevere/var-dump
- Provides vd (VarDump) and vdd (VarDump & die) helpers
- Colorful typed variable introspection
- Reports visibility
- Detects circular references
- Writes line-by-line
- Handles really large documents
- Provides caller information
- Console document
- Colorful console output (where available)
- Plain document
- Same as console (no-color)
- Same as copy HTML text
- HTML document
- Colorful representation
- Portable (we also use it on XR Debug)
💡 TL;DR: You can replace
var_dump in both CLI and HTTP context using the following helpers:
# VarDump (vd)
vd is a drop-in replacement for
var_dump. It prints information about one or more variables to the output stream.
vd($var1, $var2,); // more code
# VarDump and die (vdd)
vdd does same as
vd, but with
die(0) which halts further execution.
vdd($var); // exit()
# Advanced usage
A VarDump needs a format and an output. In the example below a VarDump object is created with console colored formatting and output:
use Chevere\VarDump\Formats\ConsoleFormat; use Chevere\VarDump\Outputs\ConsoleOutput; use Chevere\VarDump\VarDump; use function Chevere\VarDump\varDumpConsole; $varDump = new VarDump( formatter: new ConsoleFormat, outputter: new ConsoleOutput ); // Same as: $varDump = varDumpConsole();
# Instance helpers
The following helper functions can be used to initialize a VarDump object.
use function Chevere\VarDump\varDumpPlain; use function Chevere\VarDump\varDumpConsole; use function Chevere\VarDump\varDumpHtml; // Plain text $varDump = varDumpPlain(); // Colored console $varDump = varDumpConsole(); // HTML $varDump = varDumpHtml();
varDump helper can be used to access to the VarDumpInstance. If no instance has been created it will provide a console VarDump.
use function Chevere\VarDump\varDump; $varDump = varDump();
# Passing variables
withVars is used to pass
$varDump = $varDump->withVars('a var', , null);
# Shifting traces
The dump information could be affected by layers on top of VarDump, the method
withShift can be used to indicate how many previous backtrace should be removed.
$varDump = $varDump->withShift(1); // removes the first trace
process is used to trigger the var dumping process. It requires to pass a writer where the dump information will be written.
use Chevere\Writer\StreamWriter; use function Chevere\Writer\streamFor; $varDump->process( writer: new StreamWriter( streamFor('php://stdout', 'w') ) );
Chevere/VarDump/VarDumpInstance you can change the default VarDump context, which is used in
In the code below,
varDumpHtml has been passed to configure
VarDumpInstance to handle everything with HTML formatting.
use Chevere\VarDump\VarDumpInstance; use function Chevere\VarDump\varDump; use function Chevere\VarDump\varDumpHtml; // Default VarDump console $varDump = varDump(); // Set instance to VarDump Html new VarDumpInstance(varDumpHtml()); // Instance VarDump html $varDump = varDump();