October 2005
Intermediate to advanced
372 pages
11h 35m
English
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().
For example:
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");That 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"
}
}
}Start from the first element, 0, and work your way down in order to visually backtrace the steps performed ...