Indentation

All code should be properly indented. Indentation can be either three (3) spaces or a tab character. However, consistent indentation should be used. In other words, a document should be indented using only three spaces or only tab characters, but not both.

In general, lines should be wrapped at 80 characters to prevent horizontal scrolling when possible. This will also allow code to be printed without lines wrapping on the printed page. It may be helpful to use a “ruler” comment during development to help meet this requirement.

Constructs

Indentation should be used with the following constructs:

  • if/else statements
  • loops (for loops, while loops, do-while loops)
  • switch statements
  • function declarations within a class body

An additional level of indentation should be used any time one of the above constructs is nested within another.

Exmaple Code w/ Indentation

  <?php
/* Ruler comment
12345678 112345678 212345678 312345678 412345678 512345678 612345678 712345678 8
*/
 
     // Initialize our year to 2006.
     $thisYear = 2008;
 
     /***********************************
     * We set up our loop to go until  *
     * the year is no longer less than *
     * 2025.                           *
     ***********************************/
     while ($thisYear < 2025)
     {
        // Check if it's a leap year
        if($thisYear % 4 == 0)
        {
           // Print out our leap year message
           echo "The year " . $thisYear . " happens to be a leap year. What ".
                "an exciting event!\n";
        }
        else
        {
           // Print out our normal year message
           echo $thisYear . " is not a leap year.\n";
        }
 
	// Increment our year by 1.
	$thisYear++;
     }
  ?>
 
programming\indentation · Last modified: 2008/01/22 12:24 by admin
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki