Var Debug or Dump Library


/ Published in: PHP
Save to your folder(s)

A little pile of functions I use for implementing a nice var dump alternative.


Copy this code and paste it in your HTML
  1. function dd($var)
  2. {
  3. echo '<pre>' . fl() . dump($var) . PHP_EOL . PHP_EOL . 'dd() exit</pre>';
  4. exit();
  5. }
  6.  
  7. function d($var)
  8. {
  9. echo '<pre>' . fl() . dump($var) . '</pre>';
  10. }
  11.  
  12. function fl()
  13. {
  14. $dbbt = dbbt();
  15. return 'File: ' . basename($dbbt[0]['file']) . ' Line: ' . $dbbt[0]['line'] . ' - ';
  16. }
  17.  
  18. function dbbt()
  19. {
  20. $dbbt = debug_backtrace();
  21. array_shift($dbbt);
  22. array_shift($dbbt);
  23. return $dbbt;
  24. }
  25.  
  26. function dump($var)
  27. {
  28. if (is_string($var) && '' == $var) {
  29. $var = 'string(' . strlen($var) . ') ""';
  30. } elseif (is_array($var) && empty($var)) {
  31. $var = 'array(0) empty';
  32. } elseif (is_bool($var)) {
  33. $var = 'boolean ' . ((true === $var) ? 'true' : 'false');
  34. } elseif (null === $var) {
  35. $var = 'null';
  36. }
  37.  
  38. return print_r($var, true);
  39. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.