PHP | Coding Standards

Spread the love

PHP follows few rules and maintains its style of coding. As there are many coders and developers all over the world, so each of them can follow different coding styles and standards but this would have raised great confusion and difficulty for a developer to understand another developers code. It would have been hard to manage and store the code for future reference. Here is where the coding standards come into play. This not only makes a code easy to read but also makes the code very easy to refer in the future. This makes the code understandable and clearer to decipher, just like a blueprint. This also makes the code more formal and industry or software oriented. Below mentioned are few guidelines that one must follow in order to maintain the standard of PHP coding.

Here are several reasons why to use coding specifications −

  • Your peer programmers have to understand the code you produce. A coding standard acts as the blueprint for all the team to decipher the code.
  • Simplicity and clarity achieved by consistent coding saves you from common mistakes.
  • If you revise your code after some time then it becomes easy to understand that code.
  • Its industry standard to follow a particular standard to being more quality in software.

 

  • PHP tags: One must use the PHP standard tags(), rather than the shorthand tags() to delimit the PHP code. Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is required for PHP compliance and is also the most portable way to include PHP code on differing operating systems and setups.
  • Commenting: C style comments (/* */) and standard C++ comments (//) are both fine. Use of Perl/shell style comments (#) is discouraged.
  • Line length and Indentation: It is a standard recommendation to not exceed more than 75-85 characters per line of code. One must not use tabs for indentation instead use 4 spaces as it is the standard indenting method in most of the programming languages.
  • Structuring the control flow statements: The control flow or conditional statements must be written in such a way so that it could be differentiated from function call statements. While writing if, for, while, switch and other control flow statements there must be one space between the keyword and the opening parenthesis

 

  • Make Functions Reentrant − Functions should not keep static variables that prevent a function from being reentrant.
  • Alignment of Declaration Blocks − Block of declarations should be aligned.
  • Short Methods or Functions − Methods should limit themselves to a single page of code.

 

  • Naming Variables: Here are a few conventions that one must follow in order to name the variables:
    • Use of lower case letters to name the variables.
    • Use of ‘_’ to separate the words in a variable.
    • Static variable names may be started with a letter ‘s’.
    • Global variable names must start a with letter ‘g’.
    • Use of upper-case letters to define global constants with ‘_’ as a separator.
  • Block alignment: Every block of code and curly braces must be aligned.

 

 

 

You may also like...

1 Response

  1. October 28, 2018

    […] PHP Coding StandardsMagic Constant […]

Leave a Reply

Your email address will not be published. Required fields are marked *