Simple Coding Style for PHP - Part 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

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.