4
dump
dump
SYNOPSIS
dump($var, $label=null, $echo=true)
DESCRIPTION
dump
returns a string containing the readable representation of $var
in an HTML pre
tag.
If $echo
is true
, which is the case by default, the string is inserted in the output flow.
Assign a text to $label
, typically the name of a variable, to label the output.
CODE
- function dump($var, $label=null, $echo=true) {
- $label = ($label===null) ? '' : rtrim($label) . '=';
- ob_start();
- var_dump($var);
- $output = ob_get_clean();
- $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
- if (PHP_SAPI == 'cli') {
- $output = PHP_EOL . $label . $output . PHP_EOL;
- }
- else {
- $output = htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
- $output = '<pre class="dump"><code>' . $label . $output . '</code></pre>' . PHP_EOL;
- }
- if ($echo) {
- echo $output;
- }
- return $output;
- }
Comments