Skip to content

Core\Debug::dump

Writes what a value really is to standard error, so you can look at it while a program runs.

You can pass any number of values in one call. For each one, Core\Debug::dump writes a line that names the type, the length of every string and the class of every object. A control character such as a tab is made visible, and a very deep or very long structure is cut short. A value that points back at itself is marked, and a property declared secret is shown as [redacted].

On a terminal that shows colour, each type has its own colour. NO_COLOR and CLICOLOR_FORCE change this the same way they change Core\Cli::colorDepth().

A dump never goes to standard output, and Core\Out::capture never captures one. So a dump you forget to remove cannot change a response. Inside a web request, a dump is a log line at the debug level instead. On a server in production, the debug viewer shows only the type and size of each dumped value.

The examples below show a dump of the settings a program reads, a dump inside a loop, and a dump beside output that another program reads.

Parameters

  • ...$valuesmixedvariadicAny number of values. Control bytes and bidirectional text characters are shown as visible text. A deep or long structure is shortened, and a cycle is shown as a cycle. A secret property is hidden.

Return value

Returns void. Nothing. A call with no arguments writes nothing. Core\Out::capture never captures a dump.

Changelog

VersionChange
0.0.6A production server records no dumped value, only its file and line and the type and size of each value. In development, a recorded JSON or form field named password, secret, token or api_key is replaced with [redacted]. The Settings page of the debug viewer lists the newest 50 sign-ins.

Examples

See what a value is

<?nvs
// Shows `Core\Debug::dump`. It writes what a value is to standard error, so
// the program's own output does not change. Run this program to see the dump
// in your terminal.
// The settings a small program reads when it starts. They come from a
// configuration file, so every value is text.
array<string> $settings = [
'host' => 'example.com',
'port' => '8080',
'retries' => '3',
];
// `dump` writes the whole array to standard error. It names the type of every
// value, so you can see that the port here is text and not a number.
Core\Debug::dump($settings);
echo 'connecting to ', $settings['host'], "\n";
Output
connecting to example.com

Only the rows that fail

<?nvs
// Shows a `Core\Debug::dump` inside a loop. It writes only the rows that fail
// a check, so you see the bad data without printing all of it.
// Three rows from a file the program is importing. The second one has no
// email address.
array<array<string>> $rows = [
['Ada Lovelace', 'ada@example.com'],
['Grace Hopper', ''],
['Alan Turing', 'alan@example.com'],
];
int $line = 0;
int $imported = 0;
foreach ($rows as array<string> $row) {
$line = $line + 1;
if (Core\Str::length($row[1]) == 0) {
// One call takes several values. The line number and the row are
// written to standard error together.
Core\Debug::dump($line, $row);
continue;
}
$imported = $imported + 1;
}
echo 'imported ', $imported, ' of ', Core\Arr::count($rows), "\n";
Output
imported 2 of 3

A dump that cannot spoil your output

<?nvs
// Shows why a `Core\Debug::dump` is safe to leave in a program whose output
// another program reads. The dump goes to standard error. Standard output
// stays valid JSON.
array<string> $report = [
'status' => 'ok',
'region' => 'eu-central',
];
// This line goes to standard error only. A command such as
// `nvs run report.nvs | jq` still works, because the JSON below is untouched.
Core\Debug::dump($report);
echo Core\Json::encode($report), "\n";
Output
{"status":"ok","region":"eu-central"}