PHP follows few rules and maintains its style of coding. As there are many developers all over the world, if each of them follows different coding styles and standards this will raise great confusion and difficulty for a developer to understand another developer’s code. It will be very hard to manage and store the code for future reference.
The coding standards come into play for all the reasons.
Reasons for maintaining coding standards:
- People working in different modules can easily manage the source code due to familiarity.
- Acts as a blueprint for other team members to understand the code for management or changes.
- Clarity saves a lot of time doing common mistakes as it’s easy to understand.
- Coding standards or industry standards improve the quality and consistency of software.
Below mentioned are few guidelines that one must follow in order to maintain the standard of PHP coding.
- PHP tags: One must use the PHP standard tags, rather than the shorthand tags to delimit the PHP code.
Full tags :
<?PHP PHP code ?>
<? PHP code ?>
- Comments: Use of standard C and C++ commenting style i.e., (//) for single line and (/* */) for multiple-line, is highly encouraged and use of Python or Perl style of commenting i.e., (#), 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 spaces as it is the standard indenting method in most programming languages. One statement per line or else split lines for long multiple ones.
- Control structures: The control flow or conditional statements must be written in such a way so that they could be differentiated from function call statements. While writing if, for, else, foreach, do, while, switch, and other controls flow statements there must be one space between the keyword and the opening parenthesis. The function definitions do not have any space between the function name and opening parenthesis.
- Function calls: There should be no spaces between parameters, comma, last parameter, and semicolon. Always use full PHP tags and avoid shorthand tags. Also, give one space between both sides of ‘=’ operator.
- Naming variables: There are 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 the letter ‘s’.
- Global variable names must start with the 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.
- Short Functions: All functions and methods must limit themselves to a single page and must not be lengthy.
Note: The above rules are provided just to make the code easily understandable and to manage the code easily. There will not be any specific error in case of violation of the rules mentioned above. But it is highly recommended following the above convention to improve the overall quality of code from a developer’s perspective.