Hudzilla.org - the homepage of Paul Hudson
Contents > Writing PHP > Debugging Wish List | Report Bug | About Me ]

19.8.8     Backtracing your code: debug_backtrace()

This is NOT the latest copy of this book; click here for the latest version.

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!





<< 19.8.7 Exception handling   19.8.9 Debuggers >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Sep 2008

A nice real life example would be an SQL query function

function query($query, $link = false) {
$res = <your query code>
if (!$res) {
$back = debug_backtrace();
die("
SQL Error executing query: $query
Called from: $back[0][file]
Calling line: $back[0][line]
} else {
return $res;
}
}

For those of us who have PHP mail us any errors it encounters, this is invaluable in debugging.

Harshit Sekhon / bigduke.sekhon@gmail.com - 05 Sep 2008

Ahhh! this is interesting, makes life so much easier. However, could you cite a real life example as to how to make use of this handy function in both OO and procedural blocks of code?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow