Community Documentation

debug

7 common.inc debug($data, $label = NULL, $print_r = FALSE)
8 common.inc debug($data, $label = NULL, $print_r = FALSE)

Outputs debug information.

The debug information is passed on to trigger_error() after being converted to a string using _drupal_debug_message().

Parameters

$data: Data to be output.

$label: Label to prefix the data.

$print_r: Flag to switch between print_r() and var_export() for data conversion to string. Set $print_r to TRUE when dealing with a recursive data structure as var_export() will generate an error.

File

includes/common.inc, line 7296
Common functions that many Drupal modules will need to reference.

Code

<?php
function debug($data, $label = NULL, $print_r = FALSE) {
  // Print $data contents to string.
  $string = check_plain($print_r ? print_r($data, TRUE) : var_export($data, TRUE));

  // Display values with pre-formatting to increase readability.
  $string = '<pre>' . $string . '</pre>';

  trigger_error(trim($label ? "$label: $string" : $string));
}
?>

Comments

If debug hits a bug you can't debug because debug hits a bug...

If you find yourself stuck unable to debug a bug in an object because a bug in the object breaks the debugger... for example, if an object carelessly uses == inappropriately causing an infinite loop and a Fatal Error: Nesting level too deep - recursive dependency? error on debug... here's some code that is ugly but better than nothing and at least gives you somewhere to start nosing around (replace $entity with the object in question):

  foreach ($entity as $property => $value) {
    $value_string = is_string($value) ? '"'.$value.'"' : "[".gettype($value)."]";
    debug( $property . " - " . $value_string);   
  }

Actually, ignore that...

(not sure why I can edit this comment but not my previous comment...)

Actually this error is the "an error" eluded to above that the $print_r flag is for. So, if you're getting the "Nesting level too deep - recursive dependency" error, just do...

debug($entity, 'Some label', true);

...which is equivalent to...
debug(print_r($entity, true), 'some label');

Login or register to post comments