Debugging 101
Page 1 of 5 123 ... LastLast
Results 1 to 15 of 61

Thread: Debugging 101

Hybrid View

  1. #1
    Parse error line -1 Bunkermaster's Avatar
    Join Date
    Sep 2002
    Location
    France
    Posts
    1,769

    Exclamation Debugging 101

    As mentioned in a post I recently read http://www.phpbuilder.com/board/show...eadid=10240313 (not all of it, it seams some people can't stop talking here so fast it's like IRC now) some people have NO IDEA whatsoever about debugging and actually finding errors so here are my useless hints :

    1. When you code a MySQL query use new lines :
    PHP Code:
    $sql "SELECT
            `yourtable`,`field1` as f1,
            `yourtable`,`field2` as f2
            FROM
            `yourtable`
            "

    Why? If you have an error MySQL will give you line number referencing a line INSIDE the query. Always saves time to actually be able to use the info.

    2. When you code a MySQL query echo it in a HTML comment (not in production) :
    PHP Code:
    $sql "SELECT
            `yourtable`,`field1` as f1,
            `yourtable`,`field2` as f2
            FROM
            `yourtable`
            "
    ;
    echo 
    "<!--\n$sql\n-->"
    Why? Well if your pages doesn't work try running your querries in phpMyAdmin or your DB manager and see the output. It sometimes is interesting.

    More soon (tired now)
    Last edited by Bunkermaster; 05-23-2003 at 03:34 AM.

    "Our ASP will blot out the sun!"
    "Then we will PHP in the shade"

  2. #2
    Parse error line -1 Bunkermaster's Avatar
    Join Date
    Sep 2002
    Location
    France
    Posts
    1,769
    add your tricks here, it is NOT c losed thread (or is it? /me checks) ok it is NOT a closed thread. Just use same coloring as i did so peeps can look at it fast

    [COLOR=#FF8000] for tricks titles

    Code goes inside [PHP] blocks

    "Our ASP will blot out the sun!"
    "Then we will PHP in the shade"

  3. #3
    Senior Member
    Join Date
    Feb 2003
    Posts
    508
    Comment closing brackets {}

    PHP Code:
    if (isset($_COOKIE["me"])) {
           Do 
    all the code in here
               
    if ($_COOKIE["me"] == 'poser') {
                   Do 
    all the code in here
               
    } else {
                   Do 
    all the code here
               
    //Poser if
            
    } else {
                   Do 
    all the code here
            
    //Is Cookie Set 

    Why? Because as applications get more and more nested this will help you solve the unexpected $end on line number XXX error and will help you figure out where the close } is.
    Last edited by Bunkermaster; 05-23-2003 at 02:20 PM.

  4. #4
    Parse error line -1 Bunkermaster's Avatar
    Join Date
    Sep 2002
    Location
    France
    Posts
    1,769
    Did a slight edit here to remove extra lines in code (shows double in FireBird each time... Annoying)

    "Our ASP will blot out the sun!"
    "Then we will PHP in the shade"

  5. #5
    50,000 Watts of Goodwill goldbug's Avatar
    Join Date
    May 2003
    Location
    Rummaging through your garbage.
    Posts
    1,315
    It's probably been mentioned before elsewhere, but when constructing strings, sql queries, whatever,....I prefer to escape out of the string (and use single-quotes where possible):
    PHP Code:
    $moo 'This is what ' $cow 'said to me.'
    Instead of:
    PHP Code:
    $moo "This is what $cow said to me." 
    WHen using an editor that syntax-highlights, it makes it MUCH easier to pick out the variables (especially in large or complex strings).
    Many eyes make few mistakes

    goldendance

  6. #6
    Parse error line -1 Bunkermaster's Avatar
    Join Date
    Sep 2002
    Location
    France
    Posts
    1,769
    Yes goldbug but you have made the typical error

    PHP Code:
    // No space after $cow
    $moo 'This is what ' $cow 'said to me.';
    // Space after $cow
    $moo "This is what $cow said to me." 
    hehe but good one especially when embedding in HTML strings.

    "Our ASP will blot out the sun!"
    "Then we will PHP in the shade"

  7. #7
    NULL Mordecai's Avatar
    Join Date
    Mar 2003
    Location
    Springfield, MO, USA
    Posts
    2,020

    Remove headers!

    Remove all headers when you are about to debug. Specifically if you are working on an image script. If you output a PNG (or another format) using header("Content-type: Image/PNG"), then no errors can be echoed. Errors can only be seen if something is text/html, not an image. This just results in a broken image, since PHP still echoes out the error.

    Edit: Uhh.. I don't know how to use the board's code in the subject, so I took out the [color] thing.
    - cai
    caiPHP

  8. #8
    Senior Member xblue's Avatar
    Join Date
    Dec 2002
    Location
    Europe
    Posts
    1,100

    debugging parse errors:

    indent your code
    this helps to detect missing braces, and can help to spot logical errors, too.

    use an editor with syntax highlighting
    makes it easy to find unclosed strings or unescaped quotes within string.

    [bunkermaster, I think this might be more often needed in the newbie or general forum, maybe other moderators would be willing to place a reference there?]
    No keyboard found. Press F1 to continue...

  9. #9
    Senile Member
    Join Date
    Oct 2002
    Location
    Cheshire, UK
    Posts
    1,658

    Check variable contents

    Check variable contents

    Use something like this to check you received what you expected to receive

    PHP Code:
    echo '<pre>';
    print_r ($_POST);
    echo 
    '</pre>'
    Last edited by Bunkermaster; 05-27-2003 at 06:17 AM.
    Barand

  10. #10
    Senior Member Tekron-X's Avatar
    Join Date
    Oct 2002
    Posts
    294
    WORK BACKWARDS

    Most know this already but for those that don't...

    When PHP spitts an error line and number out at you go from the line number given and if the error is not in that line work backwards.

    The Reason for this is that PHP is not always accurate on its error line numbers.
    Tek

  11. #11
    Member GilesGuthrie's Avatar
    Join Date
    Jun 2003
    Location
    Edinburgh, UK
    Posts
    43
    Check for double-equals ==

    If your conditional statement isn't working properly, and it's testing for equivalence, check that you've got two equals signs.
    Take a break on the web at giles-guthrie.com

  12. #12
    Senior Member toplay's Avatar
    Join Date
    Dec 2003
    Location
    California, USA
    Posts
    597
    Quote Originally Posted by GilesGuthrie
    Check for double-equals ==

    If your conditional statement isn't working properly, and it's testing for equivalence, check that you've got two equals signs.
    I'm just going to expand on this and share a tip that would help if you've been using literals and constants on the right side of an 'if' expression. Example code:
    PHP Code:
    $value 2;
    if (
    $value 1)
        echo 
    'one';   // This gets displayed
    elseif ($value 2)
        echo 
    'two'
    The equal comparison operator in PHP requires two equal signs (==) and not just one (=). One equal sign is an assignment operator. In the example code, the '1' was assigned to the $value variable at the first 'if' expression, and this made the expression evaluate to TRUE; causing the echo 'one' to be executed and displayed. With this type of code example, PHP produces no errors (when there's a variable name to the left of the equal sign). This makes debugging this kind of problem more difficult and potentially time consuming. It's not always very apparent.

    Here's a tip to avoid this problem in the future. Get in the habit of coding the 'if' expression with the literal value or constant first or to the left of the equal sign, like this:
    PHP Code:
    $value 2;
    if (
    == $value)
        echo 
    'one';
    elseif (
    == $value)
        echo 
    'two';  // This gets displayed 
    That way if you happen to make a mistake and put just one equal sign again, then PHP will give you a parse error. The code of '1 = $value' is invalid because PHP can't assign the contents of $value to '1' (a literal; it must be a variable). This could potentially save you lots of time in testing/debugging because you get an error alert.

    Quote Originally Posted by barand
    Check variable contents
    Use something like this to check you received what you expected to receive

    PHP Code:
    echo '<pre>';
    print_r ($_POST);
    echo 
    '</pre>'
    That could be done in one line:
    PHP Code:
    echo '<pre>'print_r($_POSTTRUE), '</pre>'
    hth.
    Last edited by toplay; 08-03-2005 at 05:09 AM.
    PHP Manual 1 2 | PHP Func/Ver List | MySQL Manual | Articles 1 | Snippet Library | Tips | How to ask

    Regex/PCRE: 1 2 3 4 5 6 7 8 9 | MySQL: 1 2 3 4 5 6 7 8 9 | PHP Security: 1 2 3 4 5 | PHP Compiler: 1 2

    FAQ | PHP GUI: 1 2 | PHP Magz 2 | "Just keep coding, coding, coding..." by toplay

  13. #13
    tryin hard 2 help kakki's Avatar
    Join Date
    Aug 2005
    Posts
    327
    or use // */ comments in your code, to check/uncheck it

    used code:
    PHP Code:
    echo '<pre>';
    print_r ($_POST);
    echo  
    '</pre>';
    // */ 
    unused code
    PHP Code:
    /*
    echo '<pre>';
    print_r ($_POST);
    echo  '</pre>';
    // */ 

  14. #14
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,774
    Cut-and-paste code does not work

    As the programmer, it is your responsibility to understand what your code is doing and what it is supposed to do; the computer is not capable of doing that for you. Taking chunks of other people's code and inserting them into yours without bothering to work out how to use them is a sure way of turning your code into an unworkable mess.

    If it's a class, study its interface to see what it provides and then decide if you can use it to do what you're wanting. If it's just a fragment of code of the sort that is posted here to illustrate a solution, remember that it is just an illustration and not necessarily the real thing. Who knows what it will do when you run it? If you don't, you're in trouble.
    Last edited by Weedpacket; 03-03-2007 at 08:19 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  15. #15
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,774
    Originally posted by Tekron-X
    The Reason for this is that PHP is not always accurate on its error line numbers.
    I wouldn't say that; I'd say that PHP reports the number of the line it was processing when it discovered that there was an error (if you miss out a }, PHP can't tell you where it should have been). So , depending on the error message, even if the error itself is not on the line indicated, that line may provide clues as to where the error actually is. For example, if it says that there was a type conversion error (where, say, an array was used where it expected a string), then one of the variables in that line is at fault: work backwards and see where they get their values from.
    Last edited by Weedpacket; 06-14-2003 at 07:26 AM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •