php debug print backtrace code example

Example: php debug_backtrace

Giving a basic example...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "Start...";
print_r(debug_backtrace());

function t1 ()  {
    echo "Start t1...";
    print_r(debug_backtrace());

}

function t2()   {
    echo "Start t2...";
    print_r(debug_backtrace());
    t1();
}

echo "before calls...";
print_r(debug_backtrace());
t1();
t2();
Will output...

Start...Array
(
)
before calls...Array
(
)
Start t1...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 22
            [function] => t1
            [args] => Array
                (
                )

        )

)
Start t2...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 23
            [function] => t2
            [args] => Array
                (
                )

        )

)
Start t1...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 17
            [function] => t1
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 23
            [function] => t2
            [args] => Array
                (
                )

        )

)
I hope this shows that all the debug_backtrace function does is return what has been called so far. So when you first call t1, it is simply a stack trace of the call to t1. The same for the start of t2, but when t2 calls t1, the trace lists the call to t2 and t1.

But also as you can see, that debug_backtrace from Start.. shows nothing, as there are no levels of code that have caused this trace to be printed.

In your case it calls debug_backtrace and uses the or die() to say 'if debug_backtrace returns nothing, then die()'

Tags:

Php Example