PHP Reference

Quick Reference

The PHP debug_backtrace() function generates a PHP backtrace which displays data from the code that led up to the debug_backtrace() function.

The possible returned elements are:

  • Function (string) – The current function name
  • Line (integer) – The current line number
  • Files (string) – The current file name
  • Class (string) – The current class name
  • Object (object) – The current object
  • Type (string) – The current call type:
    • Returns: “->” – Method call
    • Returns: “::” – Static method call
    • Returns nothing – Function call
  • Args (array) – If inside a function, it lists the functions arguments; if inside an included file, it lists the included file names
<?php
function a($txt) {
    b('Hello');
}

function b($txt) {
    c('World');
}

function c($txt) {
    var_dump(debug_backtrace());
}

a('Chicken');
?>

Output

array(3) { [0]=> array(4) { ["file"]=> string(21) "/home/test.php" ["line"]=> int(7) ["function"]=> string(1) "c" ["args"]=> array(1) { [0]=> string(5) "World" } } [1]=> array(4) { ["file"]=> string(21) "/home/test.php" ["line"]=> int(3) ["function"]=> string(1) "b" ["args"]=> array(1) { [0]=> string(5) "Hello" } } [2]=> array(4) { ["file"]=> string(21) "/home/test.php" ["line"]=> int(14) ["function"]=> string(1) "a" ["args"]=> array(1) { [0]=> string(7) "Chicken" } } }

Syntax

debug_backtrace(options, limit);

Parameters

ParameterDescription
optionsSpecifies a bitmask for the following 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)

limitLimits the number of stack frames printed. By default (limit=0) it prints all stack frames

PHP Notes:

  • When using PHP, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.