Skip to content

PHP Coding Standards and Best Practices

Coding Standards place a vital role in any programming language for achieving a high code quality and to write more readable and clean code.

Here is the list of some best coding standards.

  • Upgrade to the Latest Version of PHP
  • Use only regular PHP Opening and closing tags, don’t use short tags and other php opening and closing tags.
    Ex :
    Bad
    
    
    Good
    
    
  • Indent code properly.
  • The closing ?> tag MUST be omitted from files containing only PHP.
  • PHP keywords must be in lowercase,The PHP constants true, false, and null MUST be in lower case
  • Always comment your code
  • Avoid unnecessary Variables(not copy variables unnecessarily)
    Ex:
    Bad
        
    
    Good
        
    
  • Naming conventions:

    • Variable Naming Conventions:

      • Case should never be used to differentiate between variable names. Every variable name in the current scope should be absolutely unique. Variable names
        should describe the content that they (will) contain, using either complete words or understandable abbreviations.
      • Use "lowerCamelCase" style (lowercase lettering on initial and capitalization on subsequent words) to name variable.
      • Global variables should be prepended with a 'g'.
      • Global constants should be all caps with '_' separators
    • Class Name Naming Conventions:

      • Class Name Should Start with Uppercase Letter
      • Class name should only contain alphanumeric characters. Numbers are permitted in class names but are discouraged in most cases.
        Underscores and Uppercase letters are permitted in place of the word separator(first letter of each word)
    • Constant Naming Conventions:

      • Constant Name shloud be Uppercase and we can separte each word with underscore.
    • Method and functions Naming Conventions:

      • Method names MUST be start with lowercase.
      • use unserscore or CaseChange methods to separate words
  • Comments

    • C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is discouraged.
  • Database

    • Reduce the Number of Database Queries

There could be many more points which should be considered while writing your PHP program. Over all intension should be to be consistent throughout of the code and it will be possible only when you will follow any coding standard.

Feel Free to Add Your Thoughts via Comments .

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments