Backtracing your code

array debug_backtrace ( void )

It is all well and good to see the line where your code went wrong, but how did it get to that line? Debugging complex scripts can sometimes be a nightmare because objects call functions, which call other objects and other functions, and so on - you end up with a nest of calls that make tracing the problem difficult. To make your life easier, you can use the function debug_backtrace() to tell you about the chain of events that led up to the call to debug_backtrace().

Consider the following code:

<?php
    function A($param1, $param2) {
        B("bar", "baz");
    }

    function B($param1, $param2) {
        C("baz", "wom");
    }

    function C($param1, $param2) {
        var_dump(debug_backtrace());
    }

    A("foo", "bar");
?>

As you can see, the script calls function A(), which calls B(), which calls C(), which var_dump() s the output from debug_backtrace(). Now, what debug_backtrace() will return is an array of the steps that occurred in getting to it, so that script should output the following:

array(3) {

[0]=>

array(4) {

["file"]=>

string(20) "C:\php\backtrace.php"

["line"]=>

int(6)

["function"]=>

string(1) "C"

["args"]=>

array(2) {

[0]=>

&string(3) "baz"

[1]=>

&string(3) "wom"

}

}

[1]=>

array(4) {

["file"]=>

string(20) "C:\php\backtrace.php"

["line"]=>

int(3)

["function"]=>

string(1) "B"

["args"]=>

array(2) {

[0]=>

&string(3) "bar"

[1]=>

&string(3) "baz"

}

}

[2]=>

array(4) {

["file"]=>

string(20) "C:\php\backtrace.php"

["line"]=>

int(11)

["function"]=>

string(1) "A"

["args"]=>

array(2) {

[0]=>

&string(3) "foo"

[1]=>

&string(3) "bar"

}

}

}

I have left the original whitespace in there so you can see clearly what is going on. Start from the first element, 0, and work your way down in order to visually backtrack the steps performed before debug_backtrace() was called. Each element in the return from debug_backtrace() is an array of values that together form a "step" - here is how it works:

  1. The first element (step) has a "file" of c:\php\backtrace.php, which means this is where the code was at this step. "Line" is set to 6, and "function" is set to "C", which means that on line 6 of c:\php\backtrace.php, C() was called. There is also an "args" array containing "baz" and "wom" - the two parameters passed into C().

  2. The second element tells us that B() was called on line three of the same script, with the parameters "bar" and "baz".

  3. The third element tells us that A() was called on line 11 of the same script, with "foo" and "bar" passed in.

That is the complete contents of the array, but you can see it has told us exactly how it got to where it was, including all the parameters passed into functions. This is invaluable for tracking down bugs, particularly when bad parameters are being passed into functions. Having the "file" element in each step also means that it works very nicely across multiple files, so even the most complicated scripts are brought to heel with debug_backtrace() - it is certainly worth keeping to hand!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Debuggers >>

Previous chapter: Exception handling

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.