PHP Debugging Tutorial - Your Arsenal
(Page 4 of 5 )
Not getting errors on the page, but it's simply not doing what you expect it to? print_r(), print(), exit() and IE's View Source (alt-v-c) are your friends. use them well and they'll uncover a wealth of information.
Example Situation : You're posting information from a form, but nothing seems to happen on resulting page.
Diagnosis : Use print($yourvariable) in the function you're trying to use it. If empty, ask yourself why. If you're still baffled, simply do print_r($GLOBALS); exit(); view source, and see if you can find your posted variable in the resulting mess. You should be able to pick out what you're doing wrong from a print_r($GLOBALS) 95% of the time, whether it be posting files, variables, arrays, radio boxes, checkboxes, query strings, etc. quite quickly.
Remember print("<pre>"); print_r($array); print("</pre>"); exit(); can be very useful when working with rather complex arrays or regular expressions (that returns the results in an array)
Make sure you test all of your functions thoroughly before actually using them (except when you start being able to think in code and have no need to test anything coz you simply know it's going to work, like I do at work ;P). Call the function, print results, check the database to make sure that it did what it was supposed to, etc. Try it from all different angles. Once you _know_ a function is working, it takes quite a bit of modding for it to just break.
Next: In closing >>
More Miscellaneous Articles
More By Codewalkers