PHP | Coding Standards - GeeksforGeeks

17 Feb, 2021
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
?>                
   Short tags:
<?
  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.
   
Example: 
PHP
<?php $n = 5;if ($n > 0){    echo "Positive";}elseif ($n < 0){    echo "Negative";}else{    echo "Zero";} ?>
Output: 
Positive
 Example:  
PHP
<?php echo testFunc(5, 6); function testFunc($num1, $num2) {   $val = $num1 + $num2;   return $val;} ?>
Output:  
11
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.
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. 
ManasChhabra2
geetanjali16
PHP-basics
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to execute PHP code using command line ?
PHP in_array() Function
How to convert array to string in PHP ?
How to pop an alert message box using PHP ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?