How does one go about debugging PHP?

I have an open source CMS php web site. The template / theme that I have has some issues. I have tried looking online for a solution along the expected and typical paths but they have reached a dead end.

So I have decided to do it the hard way and actually get into the code and find the error

Any suggestions on how I should begin?

    The simplest way is to just echo out variable values (I would recommend using functions like [man]var_dump/man and [man]print_r/man coupled with <pre></pre> tags for things like arrays and objects), or if that's not possible, use [man]error_log/man to dump the result to the PHP error log. There are definitely debugging tools out there (some are included in IDEs like Komodo IDE and Eclipse) but I have never really needed anything beyond the two methods I listed here. Some would consider them primitive, but they get the job done for me.

      You can use [man]debug_backtrace/man and [man]print_debug_backtrace/man to get info about the call stack and such, for when you need to know how things got into the situation they're in when it doesn't do what it's supposed to do.

      As to the preceding reply, I have a keyboard macro in my editor to take whatever is in the clipboard and stick it into the following:

      die("<pre>".var_dump($var_name_in_clipboard, 1)."</pre>");
      

      (Appropriately enough, it's Ctrl-Alt-D. 🙂 )

        There are other suggestions in the [thread=10240608]Debugging 101[/thread] thread. I myself am quite fond of Xdebug.

          Weedpacket;11032805 wrote:

          There are other suggestions in the [thread=10240608]Debugging 101[/thread] thread. I myself am quite fond of Xdebug.

          I discovered Xdebug fairly recently in my programmer life; it's been VERY helpful at times; especially as a project gets more complex and you need to trace vars/objects/properties through a process iteration.

          echo() and print_r() still work on SO many things, though; I really think use of the CLI should be something a programmer is required to learn as well.

          Lastly, and the real reason for this post: LOG FILES, BABEH!!! 😃

          For devel servers, I set this in php.ini:

          error_reporting = E_ALL 
          error_log = /var/log/php_error_log

          Boom! Always a place to look for errors. 🙂

          Just make ABSOLUTELY SURE you've got disk space, especially if you've got a debugging install of PHP (which you will have if you have Xdebug set up) 😉 😉

            I love using log files as well. Coincidentally, today at work I installed Tail for Win32 (it's a "port" of the tail command for Linux). It allows you to watch a file in real time, which is perfect for log files. My only gripe with it is that the entries are not on newlines, but when I view the error log itself in Notepad, the entries ARE on new lines. So I love it but I also hate it because it just looks like a wall of text instead of a well-organized log file.

            NogDog;11032799 wrote:

            As to the preceding reply, I have a keyboard macro in my editor to take whatever is in the clipboard and stick it into the following:

            die("<pre>".var_dump($var_name_in_clipboard, 1)."</pre>");
            

            (Appropriately enough, it's Ctrl-Alt-D. 🙂 )

            die() is probably my favourite function name (yes, it's a language construct), although WordPress does have a function called doing_it_wrong().

            I have a similar code snippet in my editor. I type "print" and then press ctrl-t and it prints...

            echo '<pre>'.print_r(VARIABLE, true).'</pre>';

            ...but the cursor is automatically positioned and highlighted where you enter the variable. It's extremely useful.

              Write a Reply...