Computer >> Computer tutorials >  >> Programming >> PHP

debug_backtrace() function in PHP


The debug_backtrace() function generates a backtrace. It returns an associative array. The possible returned elements are as follows −

NameTypeDescription
functionstringThe current function name.
lineintegerThe current line number.
filestringThe current file name.
classstringThe current class name.
objectstringThe current Object.
typestringThe current call type. If a method call, "->" is returned. If a static method call, "::" is returned. If a function call, nothing is returned.
argarrayIf inside a function, this lists the functions arguments. If inside an included file, this lists the included file name(s).

Syntax

debug_backtrace(options, limit)

Parameters

  • options − A bitmask for the below given options −

    • DEBUG_BACKTRACE_PROVIDE_OBJECT: Whether or not to populate the "object" index
    • DEBUG_BACKTRACE_IGNORE_ARGS: Whether or not to omit the "args" index, and all the function/method arguments, to save memory.
  • limit − Limit the number of stack frames printed

Return

The debug_backtrace() function returns an associative array. The possible returned elements are mentioned above.

Example

The following is an example −

<?php
   function display($str) {
      echo "Hi: $str";
      var_dump(debug_backtrace());
   }
   display('hello');
?>

Output

The following is the output −

Hi: helloarray(1) {
   [0]=>
   array(4) {
      ["file"]=> string(36) "/var/www/tutorialspoint/php/test.php"
      ["line"]=> int(8)
      ["function"]=> string(8) "printStr"
      ["args"]=> array(1) {
         [0]=>
         &string(6) "hello"
      }
   }
}