# PHP Print the Call Stack with debug_print_backtrace

> Use debug_print_backtrace() or debug_backtrace() to print the PHP call stack, with options to hide arguments and reduce memory usage via exception traces

**URL:** https://sentry.io/answers/how-do-i-print-the-call-stack-in-php/

---

## The Problem

When an error occurs in PHP and you want to understand the flow of program logic that occurred, it's useful to be able to print the call stack — all the functions that were called up to this point.

## The Solution

Use `debug_print_backtrace();`.

Below is a simple example PHP script with three functions called in sequence: `A`, `B`, and `C`. Function `C` prints the call stack:

```php
<?php
A("a");

function A($a) {
    B();
}

function B() {
    C();
}

function C() {
  debug_print_backtrace();
}
?>
```

Put this script in a file called `test.php` and run it with `php test.php`. The output is:

```
#0 /home/user/test.php(10): C()
#1 /home/user/test.php(5): B()
#2 /home/user/test.php(19): A('a')
```

This is the fastest way to print the stack trace.

### Print Without Function Parameters

The `A('a')` indicates the function `A` received parameters. You probably don't need to see this, so to make the output neater use: `debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);`.

### Print Full Details

To print the full details of every call, including parameter values, use:

```php
var_dump(debug_backtrace());
```

The output will be:

```
array(3) {
  [0]=>
  array(4) {
    ["file"]=>
    string(35) "/home/user/test.php"
    ["line"]=>
    int(9)
    ["function"]=>
    string(1) "C"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(35) "/home/user/test.php"
    ["line"]=>
    int(5)
    ["function"]=>
    string(1) "B"
    ["args"]=>
    array(0) {
    }
  }
  [2]=>
  array(4) {
    ["file"]=>
    string(35) "/home/user/test.php"
    ["line"]=>
    int(2)
    ["function"]=>
    string(1) "A"
    ["args"]=>
    array(1) {
      [0]=>
      string(1) "a"
    }
  }
}
```

To ignore function arguments, you can use `var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));`.

### Use An Exception Trace To Reduce Memory Use

Some users on StackOverflow and the PHP manual website say that `backtrace` can cause memory problems with very large applications. If you encounter this, you could instead try printing the call stack with:

```php
$e = new Exception();
var_dump($e->getTraceAsString());
```

## Further Reading

- https://www.php.net/manual/en/function.debug-backtrace.php
- https://www.php.net/manual/en/function.debug-print-backtrace.php
- https://www.php.net/manual/en/exception.gettraceasstring.php
- [Error Monitoring](https://sentry.io/product/error-monitoring/)

---

*Source: [sentry.io/answers/how-do-i-print-the-call-stack-in-php/](https://sentry.io/answers/how-do-i-print-the-call-stack-in-php/)*
