Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

debug_backtrace

Description

The debug_backtrace of Error Handling for PHP generates a backtrace.

Syntax

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

Parameters

options

A bitmask for the following options:

NumberConstantDescription
1DEBUG_BACKTRACE_PROVIDE_OBJECTWhether or not to populate the "object" index.
2DEBUG_BACKTRACE_IGNORE_ARGSWhether or not to omit the "args" index, and thus all the function/method arguments, to save memory.

limit

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

Return

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

NameTypeDescription
functionstringThe current function name.
lineintegerThe current line number.
filestringThe current file name.
classstringThe current class name.
objectobjectThe 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.
argsarrayIf inside a function, this lists the functions arguments. If inside an included file, this lists the included file name(s).

Examples

1 · void

<?

function myfunction1($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    $return = debug_backtrace();

    print_r($return);
}

myfunction1(0, 1);

?>
Array
(
    [0] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 8
            [function] => myfunction2
            [args] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

    [1] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 20
            [function] => myfunction1
            [args] => Array
                (
                    [0] => 1
                    [1] => 2
                )

        )

)

2 · options · DEBUG_BACKTRACE_PROVIDE_OBJECT

<?

function myfunction1($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    $options = DEBUG_BACKTRACE_PROVIDE_OBJECT;

    $return = debug_backtrace($options);

    print_r($return);
}

myfunction1(0, 1);

?>
Array
(
    [0] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 8
            [function] => myfunction2
            [args] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

    [1] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 22
            [function] => myfunction1
            [args] => Array
                (
                    [0] => 1
                    [1] => 2
                )

        )

)

3 · options · DEBUG_BACKTRACE_IGNORE_ARGS

<?

function myfunction1($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    $options = DEBUG_BACKTRACE_IGNORE_ARGS;

    $return = debug_backtrace($options);

    print_r($return);
}

myfunction1(0, 1);

?>
Array
(
    [0] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 8
            [function] => myfunction2
        )

    [1] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 22
            [function] => myfunction1
        )

)

4 · limit

<?

function myfunction1($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
    $myarg1 += 1;
    $myarg2 += 1;

    $options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
    $limit = 1;

    $return = debug_backtrace($options, $limit);

    print_r($return);
}

myfunction1(0, 1);

?>
Array
(
    [0] => Array
        (
            [file] => /home/user/public_html/file.php
            [line] => 8
            [function] => myfunction2
            [args] => Array
                (
                    [0] => 2
                    [1] => 3
                )

        )

)
HomeMenu