Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - PHP Debugging Tutorial
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

PHP Debugging Tutorial
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-07-26

    Table of Contents:
  • PHP Debugging Tutorial
  • Coding Style Primer
  • Common Errors
  • Your Arsenal
  • In closing

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP Debugging Tutorial - Coding Style Primer


    (Page 2 of 5 )

    First thing you need to think of is your own coding style. A coders coding style is a very personal and there are thousands of programmers out there all claiming their way is "the best".

    NOTE: The only reason i'm mentioning these things below is so that people who are not aware of the importance of coding style will find out. Having a fixed coding style will help you a heck of a lot when debugging your php code, which is why i'm mentioning it here.

    Here are a few things in your coding style that I generally feel is good practice all round :

    Comments

    <?php
    // Always comment your functions - even if just with a short description
    function commentedFunc1() {
    }

    /* Some prefer block comments */
    function commentedFunc2() {
    }

    /* But i usually use block comments if i need a lengthy description like : {
      Because this way indent folding + auto-indenting in jEdit works quite well.
    } */
    function commentedFunc3() {
    }

    // You should generally comment blocks of complex/messy code with a line or two.
    for ($c1=0;$c1&lt;(floor($height/10));$c1++) {
        
    $ratio $c1*RATIO_CONST;
        for (
    $c2=0;$c2&lt;$width;$c2 += $ratio) {
            
    // ...
            // code
            // ...
        
    }
    }

    // Commenting every single line is overkill and they tend to get in the way
    // and nearly impossible to maintain the block look correctly :
    for ($c1=0;$c1&lt;(floor($height/10));$c1++) {        // Loop the thing
        
    $ratio $c1*RATIO_CONST;            // Do the ratio maths
        
    for ($c2=0;$c2&lt;$width;$c2 += $ratio) {    // Loop the width
            // ....
            // Do something else
            // code
            // More stuff
            // ...
            // Etc.
        
    // End loop $c2
    // End loop $c1

    // I generally don't comment the end of loops either, since when you copy and paste it and
    // modify the code somewhere, you tend not to alter the comment to reflect the changes
    // properly.. that gets messy.
    ?>

    Naming conventions

    You need to grab a naming convention from somewhere so that you'll be able to remember your function/variable names far easier. Decide whether you are going to use plurals in your names, whether you're going to prefix all your variable with their type/context/usefulness/etc. But when you have, try stick to it.

    Obviously, if it's a mission sticking to your naming convention then you've chosen a far too complex one for what you need. Remember: PHP is sticky about uppercase only for variables and constants(I think), not functions. Here's the php naming convention that I personally use :

    <?php
    // I lend a lot of the naming conventions from Java since it made more sense.
    $variableNumberOne "this is a normal variable";
    $postedvariable "I use all lower case for POSTed/GETted variables. always.";

    // Common posted variable names :
    $action "I'd have a switch at the bottom of my php page to use $action to tell my page what to do";
    $conf = array("i generally have $conf as a POSTed array containing all the data from a form");
    // This means i can declare $conf global in a function (or assign $conf=$_POST["conf"]) and not
    // have to worry about declaring hundreds of variables.

    // I use the same convention as java for function names, even if it doesn't matter in php
    // because it's more legible. and i don't have time for underscores
    function functionName() {
    }

    // I use all caps and underscore to seperate words in constant names
    define("INTERESTING_CONSTANT",1);
    ?>

    One last thing. INDENT YOUR CODE! PLEASE?! Code that runs alongside the page is completely illegible and it is horrible to try and debug.

    Ok, enough of coding style. The rest (things like whether to put braces on newlines, etc.) does not really pertain to keeping your code easily debuggable, so I won't delve into it..

    More Miscellaneous Articles
    More By Codewalkers


       · put this at the top of your php... It will make your code show all errors (even if...
       · Make sure you remove error_reporting(E_ALL) or turn it off by changing it to...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway