2

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
  1. function dump($var, $label=null, $echo=true) {
  2.     $label = ($label===null) ? '' : rtrim($label) . '=';
  3.  
  4.     ob_start();
  5.     var_dump($var);
  6.     $output = ob_get_clean();
  7.  
  8.     $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  9.     if (PHP_SAPI == 'cli') {
  10.         $output = PHP_EOL . $label . $output . PHP_EOL;
  11.     }
  12.     else {
  13.         $output = htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
  14.  
  15.         $output = '<pre><code>' . $label . $output . '</code></pre>' . PHP_EOL;
  16.     }
  17.  
  18.     if ($echo) {
  19.         echo $output;
  20.     }
  21.  
  22.     return $output;
  23. }

Comments

To add a comment, click here.