HI Everyone, I have worked on VB scripting and other client side scripting languages in the past, this is the first time working on server side lang PHP. I was wondering how to debug our scripts. I am using notepad++ as editor. To run any script, we have to open it in browser. If the syntax is fine then it renders the required results, but in case something goes wrong, the browser doesn't render anything. It becomes difficult in that case to debug the scripts and to pin point the root of problem. Just wanted to know how you people do debugging. I am sure I am missing something here.
Thanks for your help... we have something like Step-In, Step out where we can run the script line by line in VBScript... Do we have something similar for PHP debugging?
Are you sure error reporting is turned on? Put this at the top of the script: ERROR_REPORTING("E_ALL");
Use a good PHP editing application, I use phpDesigner, it debugs as you code, whnever you put a wrong code it alerts you before you move further.
Thanks everyone for your response. @garrettheel: I did a lot of research today on this and found something for which I think I need to open a new thread because the topic is bit different... please have a look here... http://forums.digitalpoint.com/showthread.php?p=9531289#post9531289 @born2hack: many people have recommended notepad++ also as their PHP editor of choice...moreover it supports many languages.
the PHP Processor has embedded debugging feature when attempting to compile a script a runtime (when you upload it to the host and access it via http). this means, so long as you have error reporting on, you will get plain text error reports that will tell you basically what is wrong with your script and where the error is occurring by line number (closest to the error). it takes time to learn what most errors mean (and alot of google searching), but the most basic syntax errors are missing instances of unclosed ( ) { } and eol designator ; additionally, another well known error is: can not send header data - header already sent. This basically means you are trying to send raw headers, cookies after html ouput, headers, or cookies have already been sent somewhere previously in the script execution. An easy fix is the use of ob_start() output buffer, but i try not use that when possible and rewrite code so it isnt needed, but sometimes it is needed for buffering. For development, my personal preference is either Karlis Blumentals Webuilder 2008 (for stand-alones apps) or PHP Edit (for application systems). Both are fine applications for php development but they are commercial applications. Although the free price is appealing, i really dont like the format and layout of notepad++ with the +'s and -'s ... its jsut irritating to use, but again its free, so you get what you pay for.
some nice info there... please take some time to answer my query here.... http://forums.digitalpoint.com/showt...89#post9531289 I have +repped to everyone who answered. Thanks!
if you are familiar with firebug for firefox, there is http://www.firephp.org/ - a php 5 class that allows you do to fb($varname); etc, and using X-headers, gets picked up from firebug and displayed / expanded there. if you dont have firebug, it simply drops the headers so no danger of leaving debug code exposed to the average public.
Simple echo's and print_r for me. Of course if you're doing serious programming you may need something more heavy duty, check out zend studio and other zend solutions.
oh yes, I have started using firebug since a week back and its awesome. Would you mind telling some more details about firephp, what it can and cannot do? I looked at the website but couldn't find any info or help on it. btw +rep for your help oops! here is the link again http://forums.digitalpoint.com/showthread.php?p=9531289#post9531289
Hi there Check out the try/catch stuff and the exception handling. http://se.php.net/exceptions http://se.php.net/debug_backtrace http://www.w3schools.com/php/php_exception.asp Here is a simple class to use for general debug while testing code. debug.php <?php class debug { /** * newline constant */ const NL = "\n"; /** * The constructor does the work of this little class. Prints out the $p_mToInspect using print_r() * * @param mixed $p_mToInspect Main data to display in the debug frame */ public function __construct($p_mToInspect = false) { $aDebug = debug_backtrace(); $sHeading = $aDebug[1]['class'] . '::' . $aDebug[1]['function'] . '::' . $aDebug[0]['line']; $sDisplay = '<div style="background-color: #ccc; border: 2px #cc0000 solid; clear: both; text-align: left; padding: 5px">' . self::NL; $sDisplay .= '<!-- Debug information being printed -->' . self::NL; $sDisplay .= '<span style="font-weight: bold;">'. $sHeading . '</span><br />' . self::NL; $sDisplay .= '<pre">' . self::NL . htmlentities(print_r($p_mToInspect, true)) . self::NL . '</pre>' . self::NL; $sDisplay .= '</div>' . self::NL; return $sDisplay; } } ?> PHP: Most convinient is to include the class deep down in your application structure so it's avaliable for instantiation everywhere, or otherwise simply include it in your files. You may want to alter the return to add to a registry/namespace/log or whatever, instead of a string, also just echo $sDisplay; It depends on your application layout. Use like this new debug($something_to_debug_here); PHP:
I have created a seperate error class wich will be called after an error where found, all information will be send to my email. Further i kill the process by die('page') and view variables by print_r
I've always found that if you are having errors that produce no output, then the script itself is fundementally flawed in it's execution logic lacking proper error handling. the 'or die' methods, checking that functions actually return values and don't just assume they are wrong, etc, etc. FORMATTING also plays a key in this as a lot of the mistakes and errors people waste tools on checking for could be avoided if people just used the damned tab key in a consistant manner. Certain 'visual' environments (which admittedly I've never been able to wrap my machine code trained brain around visual programming) seem to be rife with ingraining sloppy coding habits that inherently lead to buggy bloated slow code. See the link in my sig for more.
Of course, if you're using an editor that supports it (eg. PDT or PHPEclipse) you can do proper step debugging with variable analysis and changing so long as you have a proper PHP debug extension installed on your server. Something like PHPEclipse + XDebug is probably more like what you are used to, but it takes a bit of digging to get it all working properly.