The new debug_backtrace() function is actually not documented yet even though it's in the newly released 4.3.0. Now that I have 4.3.0, I get to play.
First misconception: You'd think it would actually print a backtrace. Instead, it returns an array containing lots of nifty information that you have to print out yourself.
Given this PHP code:
<?php
function foo(){
echo "<pre>"; print_r(debug_backtrace()); echo "</pre>";
}
function bar(){
foo();
}
foo();
bar("baz");
echo "<pre>"; var_dump(debug_backtrace()); echo "</pre>";
?>
You get the following output:
Array
(
[0] => Array
(
[file] => /home/keith/public_html/dispatcher.php
[line] => 48
[function] => foo
[args] => Array
(
)
)
)
Array
(
[0] => Array
(
[file] => /home/keith/public_html/dispatcher.php
[line] => 46
[function] => foo
[args] => Array
(
)
)
[1] => Array
(
[file] => /home/keith/public_html/dispatcher.php
[line] => 49
[function] => bar
[args] => Array
(
[0] => baz
)
)
)
array(0) {
}
As you can see, when just foo is called, you get a call trace for just foo. When bar is called, you get the call trace for both foo and bar. In addition, you can see that debug_backtrace() returns nothin (but an empty array, of course) at the top level.
In addition, debug_print_backtrace(), which I assume actually prints the backtrace formatted in some way, was added in a recent version of CVS. This function didn't make it into PHP 4.3.0.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):