PHP: Debug Backtrace with Example

This article shows how we can use the debug_backtrace() PHP function to generate a backtrace. This function returns an array of associative arrays. It includes the current function name, file name, class name, line number, etc. where the debug backtrace function was called.

Syntax


debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array

Parameters


$options

- Constant value
- Value
    - DEBUG_BACKTRACE_PROVIDE_OBJECT (default)
    - DEBUG_BACKTRACE_IGNORE_ARGS

- DEBUG_BACKTRACE_PROVIDE_OBJECT
    - Whether or not to populate the "object" index.
    - The integer value of this constant is <strong>1</strong>
- DEBUG_BACKTRACE_IGNORE_ARGS
    - Whether or not to omit the "args" index.
    - The integer value of this constant is <strong>2</strong>

$limit

- Limit the number of stack frames returned
- By default (limit=0) it returns all stack frames

Return Values

This function returns an array of associative arrays. It includes the current function name, file name, class name, line number, etc. where the debug backtrace function was called.

Examples

Output with object and arguments


debug_backtrace()
OR,
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 0)
OR,
debug_backtrace(1, 0)

where,
$options = DEBUG_BACKTRACE_PROVIDE_OBJECT or 1
$limit = 0

<?php

class Greetings
{
    public function printMessage($name)
    {
        echo "Hello {$name}! \n";

        // debug backtrace
        $debugBacktrace = debug_backtrace();
        print_r($debugBacktrace);
    }

    public function sayHello($name)
    {
        $this->printMessage($name);
    }
}

$greetings = new Greetings();
$name = 'John';
$greetings->sayHello($name);

Output:


Hello John!
Array
(
    [0] => Array
        (
            [file] => /Users/mukeshchapagain/Downloads/Greetings.php
            [line] => 15
            [function] => printMessage
            [class] => Greetings
            [object] => Greetings Object
                (
                )

            [type] => ->
            [args] => Array
                (
                    [0] => John
                )

        )

    [1] => Array
        (
            [file] => /Users/mukeshchapagain/Downloads/Greetings.php
            [line] => 21
            [function] => sayHello
            [class] => Greetings
            [object] => Greetings Object
                (
                )

            [type] => ->
            [args] => Array
                (
                    [0] => John
                )

        )

)

Output without objects and arguments


debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
OR,
debug_backtrace(2)

where,
$options = DEBUG_BACKTRACE_IGNORE_ARGS or 2
$limit = 0

<?php

class Greetings
{
    public function printMessage($name)
    {
        echo "Hello {$name}! \n";

        // debug backtrace
        $debugBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        // $debugBacktrace = debug_backtrace(2);
        print_r($debugBacktrace);
    }

    public function sayHello($name)
    {
        $this->printMessage($name);
    }
}

$greetings = new Greetings();
$name = 'John';
$greetings->sayHello($name);

Output:


Hello John!
Array
(
    [0] => Array
        (
            [file] => /Users/mukeshchapagain/Downloads/Greetings.php
            [line] => 16
            [function] => printMessage
            [class] => Greetings
            [type] => ->
        )

    [1] => Array
        (
            [file] => /Users/mukeshchapagain/Downloads/Greetings.php
            [line] => 22
            [function] => sayHello
            [class] => Greetings
            [type] => ->
        )

)

Limit output array stack


// only print the first element from the array stack
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)
OR,
debug_backtrace(2, 1)

debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1)
OR,
debug_backtrace(1, 1)

where,
$options
    DEBUG_BACKTRACE_IGNORE_ARGS or 2
    DEBUG_BACKTRACE_PROVIDE_OBJECT or 1
$limit = 0

<?php

class Greetings
{
    public function printMessage($name)
    {
        echo "Hello {$name}! \n";

        // debug backtrace
        $debugBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
        // $debugBacktrace = debug_backtrace(2, 1);
        // $debugBacktrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1);
        // $debugBacktrace = debug_backtrace(1, 1);
        print_r($debugBacktrace);
    }

    public function sayHello($name)
    {
        $this->printMessage($name);
    }
}

$greetings = new Greetings();
$name = 'John';
$greetings->sayHello($name);

Output:


Hello John!
Array
(
    [0] => Array
        (
            [file] => /Users/mukeshchapagain/Downloads/Greetings.php
            [line] => 16
            [function] => printMessage
            [class] => Greetings
            [type] => ->
        )

)

Hope this helps. Thanks.