Personal notes on software development.
For Java technologies check my dedicated site

Pages

Tips

  • Turn "on" error reporting in PHP
    Error messages display the exact line of code that threw or generated an error. This makes debugging a matter of looking at the line number shown on the browser by the generated error and checking that line number in your code.
    Note: keep in mind that when your code is debugged and ready to go live, you want to make sure error reporting is turned off because you don't want visitors to your site seeing error messages that may give them enough knowledge to exploit a weakness and hack your site.

    Turn error reporting "on" in the php.ini file and set the level of error reporting.
    display_errors = On
    error_reporting = E_ALL & ~E_NOTICE
    Some different levels for error_reporting are:
    "error_reporting = E_ALL": outputs all errors, bad coding practices and warnings like uninitialized variables;
    "error_reporting = E_ALL & ~E_NOTICE":  outupts errors, any bad coding practices, but not the harmless warnings;
    Restart Apache after changing the php.ini.
  • Debugging using print statements
    Functional bugs in your application don't generate errors so a way to gain knowledge of what is going on behing the scenes is by accurately place and use print or die statements to debug your PHP.
    The die() statement halts program execution and displays text to the Web browser. The die() statement is particularly useful if you don't want to have to comment out your code, and you only want everything up to the error and the error displayed and nothing after.
    Syntax examples:
    die("i =".$i);
    print("i =".$i);
  • Use a debugger
    There are a couple of IDEs that make the debugging process easier by letting you set breakpoints and providing syntax parsing.

    One example is the Netbeans PHP plugin that provides tools and support for PHP development. Includes PHP editor, debugger, samples and documentation. It also comes with a javascript debugger built in. If you are a Java developer and already have Netbeans installed, just add the pack using the IDE's Plugin Manager (Tools | Plugins). If you dont have it installed yet, you can download the PHP exclusive version here (33MB).

    Another example is the PHPeclipse plug-in for Eclipse and the debugger PHP extension. Learn more about it in this tutorial[1].

Resources

[1] Debugging techniques for PHP programmers

No comments:

Post a Comment