spacer
Web Development Blog
 Developer Newsletter

Webmaster Blogs
Content & Blogging
Design
Photoshop
General
JavaScript
PHP
PHP Functions
Web
WordPress
Website Promotion

Blog Archives


Developer Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous

Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Webmaster Blog

Debugging PHP code using debug_backtrace

Most of the PHP developers debug php code in their local machine just by trial and error using “print_r”,”var_dump” and “echo”. They dont write unit tests or follow any advanced debugger like xdebug. But the problem of using these methods is you cannot fool proof your code and their might be some bugs still present in your code. Lets see how can we debug our code more effectively getting more information from the php interpreter itself. There is a nice function called debug_backtrace() is available in PHP to trace the root of an error. As the name implies, you can trace the execution of you code which produces the error. Have a look at the following code.


<?php
function processUserInput($a, $b)
{
echo divide($a,$b);
}
function divide($c, $d)
{
print_r(debug_backtrace());
}
echo processUserInput(4,0);
?>

This will output the following one

Array
(
    [0] => Array
        (
            [file] => PHPDocument1
            [line] => 5
            [function] => divide
            [args] => Array
                (
                    [0] => 4
                    [1] => 0
                )

        )

    [1] => Array
        (
            [file] => PHPDocument1
            [line] => 14
            [function] => processUserInput
            [args] => Array
                (
                    [0] => 4
                    [1] => 0
                )

        )

    [2] => Array
        (
            [file] => /home/hasin/Zend/ZendStudio-5.5.0/bin/php5/dummy.php
            [line] => 1
            [args] => Array
                (
                    [0] => PHPDocument1
                )

            [function] => include
        )

)


Related Posts
» The ultimate PHP web development environment, part 1
» The ultimate PHP web development environment, part 2
» PHP 6 now with .net: Visual Studio integration available already
» Packaging a Wordpress Theme for Distribution
» Code reuse in PHP frameworks
 


2 Responses to “Debugging PHP code using debug_backtrace”

  1. andPHP.com Says:

    Using debug_backtrace to speed up debuging process…

    Hasin Hayder from Developer Tutorials wrote this neat post about using debug_backtrace. It could help you debug your code more efficiently and save you hours of dumping variables and testing functions, not to mention saving you from pulling your hair…

  2. Developer Tutorials: Debugging PHP code using debug_backtrace | Cole Design Studios Says:

    […] the Developer Tutorials blog today, Hasin Hayder has pointed out a handy PHP function that can make your debugging a bit less of a headache - debug_backtrace. […]

Leave a Reply





About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: