1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do you debug PHP scripts?

Discussion in 'PHP' started by geekology, Oct 16, 2008.

  1. #1
    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.
     
    geekology, Oct 16, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I always print() the result or print_r() the array when something goes wrong.
     
    ads2help, Oct 16, 2008 IP
    geekology likes this.
  3. geekology

    geekology Active Member

    Messages:
    545
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    60
    #3
    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?
     
    geekology, Oct 16, 2008 IP
  4. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Are you sure error reporting is turned on? Put this at the top of the script:

    ERROR_REPORTING("E_ALL");
     
    garrettheel, Oct 17, 2008 IP
    geekology likes this.
  5. born2hack

    born2hack Banned

    Messages:
    294
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    born2hack, Oct 17, 2008 IP
    geekology likes this.
  6. geekology

    geekology Active Member

    Messages:
    545
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    60
    #6
    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.
     
    geekology, Oct 17, 2008 IP
  7. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    Bind, Oct 17, 2008 IP
    geekology likes this.
  8. geekology

    geekology Active Member

    Messages:
    545
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    60
    #8
    geekology, Oct 17, 2008 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    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.
     
    dimitar christoff, Oct 17, 2008 IP
    geekology likes this.
  10. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    your link is dead.

    instead of copyiing and pasting the text, copy and past Shortcut/URL
     
    Bind, Oct 17, 2008 IP
  11. rgomes

    rgomes Member

    Messages:
    22
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    38
    #11
    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.
     
    rgomes, Oct 17, 2008 IP
  12. geekology

    geekology Active Member

    Messages:
    545
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    60
    #12
    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
     
    geekology, Oct 17, 2008 IP
  13. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #13
    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:
    :)
     
    wing, Oct 17, 2008 IP
  14. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #14
    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
     
    EricBruggema, Oct 18, 2008 IP
  15. chanakya

    chanakya Peon

    Messages:
    361
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    All Php5 OOP code mixed with correct try/catch exceptions servers out purpose very well
     
    chanakya, Oct 18, 2008 IP
  16. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #16
    I just run the page. If it works I dance, else I read the error message and fix it up.
     
    blueparukia, Oct 18, 2008 IP
  17. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #17
    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.
     
    deathshadow, Oct 18, 2008 IP
  18. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #18
    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.
     
    TwistMyArm, Oct 18, 2008 IP