Today: 11/22/2008
 

Simple Coding Style for PHP - Page 2/2

 

Some more guidelines:

  • "foreach" is better than "for" which is better than "while".
  • Never use nonsense names for variables e.g. foo, sz, var and etc.
  • Try not to use global variables but if for some reason use them then all global variables and constants will be represented by capital letters e.g. $CACHE_PATH, $FILE_NAME and etc.
  • Try making classes where possible.
  • Long-lived variable (not necessarily a global, or even in the main scope; it is simply a variable that is used through any significant length of code and/or whose representation can use clarification) should have concise but descriptive names.
  • Temporary variables should be short and concise. Numeric variables used for iteration should always be named i, j, k, l, m, and n.
  • We’re not going to use tags like the following . We’ll be using or <script language="php"> </script> (work in Front Page too).
  • Use echo not print.
  • We’ll be using C and C++ style comments i.e. "//" and "/* */". We won’t use "#".
  • Comments like the following are useless so avoid using them.
    // Increment i
    $i++;
    
  • Using phpdoc for documentation.
  • For multiword names for variables, use underscores to break words.

    $num_elements = count($elements);

  • Function Names:
    They should be all lowercase, and multiword names should be separated by underscores. e.g.
    function print_hello($name)
    { 
    echo "Hello $name"; 
    }
    
  • Class Names:
    • The first letter of a class name is capitalized. This visually distinguishes a class name from a member name.
    • Underscore should be used to simulate nested namespaces.
    • Multiword class names should be concatenated, and the first letter of each word should be capitalized. e.g.
      	class XML_RSS {}
      	class Text_PrettyPrinter {}
      	
  • Method Names:
    The Java style is to concatenate words in multiword method names and uppercase the first letter of every word after the first. e.g.
    class XML_RSS 
    { 
        function startHandler() {} 
    }
    
  • Naming Consistency:

    Variables that are used for similar purposes should have similar names. e.g.

    $max_element;
    $min_element;
    $sum_element;
    $prev_item;
    $curr_item;
    $next_item;
    

  • Matching Variable Names to Schema Names:

    Variable names that are associated with database records should always have matching names.

    $query   = "SELECT  firstname, lastname, employee_id  
                     FROM    employees" 
    $results =   mysql_query($query); 
    while(list($firstname, $lastname, $employee_id) = mysql_fetch_row()) 
    { 
        //... 
    } 
    
  • Documentation:

    Use phpDocumentor to generate documentation on the fly. Please take a look at the software www.phpdoc.org.

 

Page: 1  2

=======================================================================================

Support Techs Worldwide:

Link to us:

You can support us by putting a link to our website on your blog or website (code is below).

<a href="http://www.techsww.com">
Techs Worldwide - Tutorials about Software Installation,
Configuration, Administration, Monitoring, Tools, Tips &
Tricks
</a>

OR a simple one.

<a href="http://www.techsww.com">
Techs Worldwide - IT related Tutorials
</a>

Feedbacks:

We appreciate feedbacks and suggestions about our tutorials and Techs Worldwide from readers. Please contact us using the form here and let us know what you think about the tutorial and the website in general.

Bookmark Us:

We are working on new features for the website, please keep visiting or bookmark us using your favourite bookmarking service.

Subscribe to RSS:

You can subscribe to our RSS feed here.

 
 
Creative Commons License   Valid XHTML 1.0 Transitional Valid CSS