PHP Function Reference

PHP debug_backtrace() Function



The PHP debug_backtrace() function is used to generates a PHP backtrace.

Syntax

debug_backtrace(options, limit)

Parameters

options Optional. Specify this parameter as a bitmask for the following options:
  • DEBUG_BACKTRACE_PROVIDE_OBJECT - (default) Whether or not to populate the "object" index.
  • DEBUG_BACKTRACE_IGNORE_ARGS - Whether or not to omit the "args" index, and thus all the function/method arguments, to save memory.
limit Optional. This parameter can be used to limit the number of stack frames returned. By default (limit=0) it returns all stack frames.

Return Value

Returns an array of associative arrays. The possible returned elements are as follows:

NameTypeDescription
functionstringThe current function name.
lineintThe current line number.
filestringThe current file name.
classstringThe current class name.
objectobjectThe current object.
typestringThe current call type. Possible calls:
  • If a method call, "->" is returned.
  • If a static method call, "::" is returned.
  • If a function call, nothing is returned.
argsarrayIf inside a function, it lists the functions arguments. If inside an included file, it lists the included file names.

Example: debug_backtrace() example

The example below shows the usage of debug_backtrace() function.

<?php
function test_a($x, $y){
  echo "\$x + \$y = ".($x + $y)."\n";
  var_dump(debug_backtrace());
}

test_a(10, 20);
?>

The output of the above code will be:

$x + $y = 30
array(1) {
  [0]=>
  array(4) {
    ["file"]=>
    string(39) "Main.php"
    ["line"]=>
    int(7)
    ["function"]=>
    string(6) "test_a"
    ["args"]=>
    array(2) {
      [0]=>
      int(10)
      [1]=>
      int(20)
    }
  }
}

Example: debug_backtrace() example

Consider one more example on debug_backtrace() function.

<?php
function test_a($str){
  test_b($str);
}

function test_b($str){
  echo "Hi $str \n";
  var_dump(debug_backtrace());
}

test_a('friend');
?>

The output of the above code will be:

Hi friend 
array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(39) "Main.php"
    ["line"]=>
    int(3)
    ["function"]=>
    string(6) "test_b"
    ["args"]=>
    array(1) {
      [0]=>
      string(6) "friend"
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(39) "Main.php"
    ["line"]=>
    int(11)
    ["function"]=>
    string(6) "test_a"
    ["args"]=>
    array(1) {
      [0]=>
      string(6) "friend"
    }
  }
}

❮ PHP Error Handling Reference