PHP Script Forums » General Web Development Forum

[sticky]

Simple php code debugging

(3 posts)
Please support us by buying these products:

  1. Olaf

    Olaf
    PHP Coder

    Each beginning (and more advanced) php coder will fail because of errors while writing php code. This post will teach you some simple things you can do to find errors in your code. By default some errors will not show up on your web server!

    Read the php manual
    The php manual is one of the best manuals I ever read, each function is indexed and contains comments and examples from users. Often you will find exact the snippet you need.
    Hint! just add the function name behind the domain for a quick access like:
    http://www.php.net/foreach

    Most of the php configurations will not show errors in a production environment, enable a full error report with this two directives inside your .htaccess file:

    php_value display_errors 1
    php_value error_reporting E_ALL

    If you test your database powered website there are often problems with dynamic variables within the sql statement. Test ALWAYS your statement in phpmyadmin or use at least some error reporting like:

    mysql_query("SELECT * FROM table1 WHERE id = 34") or die(mysql_error());

    Missing curly brackets
    If your script becomes bigger and bigger it's almost normal that you will forget to close some brackets. Follow this two rules to make your live easier:

    • Indent your code, each "tab" presents a bracket you have used before
    • Comment your closed brackets like \\ end if result is not empty

    There are also a lot of code editors with a "close bracket" feature.

    Test your code with "if" statements, a lot of php functions will return a "false" if the function has failed (check the manual what is a valid return value). Example:

    if (mail('you@mail.com', 'some subject', 'some message')) {
       echo 'mail send';
    } else {
      echo 'mail error';
    }

    The result is a white screen
    This blank screens are really bad and will eat your time! Test your code with "if" statements and show some message with echo 'ok'; to find out where the error is located.

    Another problem to find errors is the "@" character before some functions, the possible error for this code will not show up in the browser:
    $arr = @getimagesize($image);
    Don't use the "@" character if you test your script!

    If you have some error message you can't fix, try a search on Google for a part of this error message or ask on a forum if someone know this message. Most of the problems are not unique!

    Posted 1 year ago #
  2. miramardesign

    miramardesign
    Member

    I hate it when people put @function(), doesn't make sense to me to make a potential bug harder to kill. If my script dies even in production I would prefer to hear about it and fix it immediately.

    Posted 1 year ago #
  3. Olaf

    Olaf
    PHP Coder

    Quote from: miramardesign
    "I hate it when people put @function(), doesn't make sense to me to make a potential bug harder to kill. If my script dies even in production I would prefer to hear about it and fix it immediately."

    you're absolutely right, bad enough that most of the visitors doesn't report bugs/errors.

    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.