8.5.x common.inc debug($data, $label = NULL, $print_r = TRUE)
8.0.x common.inc debug($data, $label = NULL, $print_r = TRUE)
8.1.x common.inc debug($data, $label = NULL, $print_r = TRUE)
8.2.x common.inc debug($data, $label = NULL, $print_r = TRUE)
8.3.x common.inc debug($data, $label = NULL, $print_r = TRUE)
8.4.x common.inc debug($data, $label = NULL, $print_r = TRUE)
8.6.x common.inc debug($data, $label = NULL, $print_r = TRUE)
7.x 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().


$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.

1 call to debug()
SimpleTestFunctionalTest::stubTest in modules/simpletest/simpletest.test
Test to be run and the results confirmed.
4 string references to 'debug'
CommentInterfaceTest::setEnvironment in modules/comment/comment.test
Re-configures the environment, module settings, and user permissions.
DrupalTestCase::error in modules/simpletest/drupal_web_test_case.php
Fire an error assertion.
watchdog_severity_levels in includes/common.inc
Returns a list of severity levels, as defined in RFC 3164.
_drupal_get_last_caller in includes/errors.inc
Gets the last caller from a backtrace.


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


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));


alanom’s picture

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);    
alanom’s picture

(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');