Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Backtracing Your Code

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page