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.
- PHP tags : One must use the PHP standard tags(), rather than the shorthand tags() to delimit the PHP code.
- Commenting : Use of standard C and C++ commenting style i.e., (//) – for single line and (/* */) – for multi-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 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.
- Function Calls : While writing a function call statement, there must be no space between the function name and the opening parenthesis. Example:
- Naming Variables : Here 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 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.
- 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 to follow the above convention to improve the overall quality of code from a developer’s perspective.
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.