Every company follows a different set of coding standards depending on its best practices. The coding level is required because there may be many developers working on different modules so if they are going to start setting up their own standards then the source will be very unmanageable and it will be difficult to keep that source code in the future.


Here are a few reasons why you used coding information -

  • Your peer settings must understand the code you generate. The standard coding standard serves as the framework for the whole coding team.
  • The simplicity and clarity gained by consistent coding saves you from common mistakes.
  • If you update your code after a while it becomes easier to understand that code.
  • Its industry standard to follow a certain standard of being high quality software.

There are a few guidelines that can be followed when encoding PHP.

  • Indenting and Line Length - Use indent 4 spaces and do not use any tab because different computers use different tab settings. It is recommended to keep lines approximately 75-85 characters long for better readability of the code.

  • Control Properties - This includes if, temporarily, changes, etc. Control statements should have a single space between the control keyword and opening brackets, separating them and activity calls. You are strongly encouraged to always use curly braces or in situations where it is not legally possible.

Examples

<?php
if ((condition1) || (condition2)) {
   action1;
}elseif ((condition3) && (condition4)) {
   action2;
}else {
   default action;
}
?>


You can write switch statements as follows −

<?php
switch (condition) {
   case 1:
      action1;
      break;
   
   case 2:
      action2;
      break;
         
   default:
      defaultaction;
      break;
}
?>

  • Function Calls - Tasks should be named without spaces between the function name, opening parenthesis, and first parameter; spaces between commas and individual parameters, and there is no space between final parameters, closing parentheses and semicolon. Here is an example -

$var = foo($bar, $baz, $quux);

  • Function Definitions − Function declarations follow following example

function fooFunction($arg1, $arg2 = '') {
   if (condition) {
      statement;
   }
   return $val;
}

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

PHP Code Tags − 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.


Variable Names − 

  • Use all lower case letters 
  • Use '_' as the word separator. 
  • Global variables should be prepended with a 'g'. 
  • Global constants should be all caps with '_' separators. 
  • Static variables may be prepended with 's'. 


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. 

One Statement Per Line − There should be only one statement per line unless the statements are very closely related. 

Short Methods or Functions − Methods should limit themselves to a single page of code.