PHP Coding Standards and Guidelines

PHP - Coding Standards


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP Coding Standards.

Every company follows a different coding standard and guidelines based on their best practices. Coding standard is required because there may be many developers working on different module, so if they start developing their own standards then the source will become so un-manageable and it will become difficult to maintain that source in the future.


Following below are the several reasons why you should use coding standards -

  • Simplicity and clearity achieved from consistent coding helps you to avoid common mistakes.
  • If you revise your code after some time then it becomes very easy to understand that code.
  • Your peer programmers have to easily understand the code that you write. A coding standard act as a blue print for all the team to translate the code.

There are few guidelines which can be adhrered to while coding with PHP -

  • Indenting and Line Length - Use an indent of 4 spaces and don't use any tab because various computers make use of different settings for tab. It is recommended to keep lines at about 75-85 characters long for better code readability.
  • Control Structures - These includes if, for, while, do, etc. Control statements should have at least one space in the middle of the control keyword and the opening parentheses, to differentiate them both from function calls. You're strongly encouraged to always make use of curly braces even in situations where optional.

Example

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

You can write switch statement as follows -

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


  • Function Calls - PHP functions should be called with no spaces between the function name, opening parenthesis, and first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis and semicolon.

Example

The following below is a simple example -

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

  • Function Declarations - The function declarations follow the "BSD/Allman style" -

Example

The following below is a simple example -

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


  • Comments - You can make use of C style comments (/* */) and standard C++ comments (//) in your code. The use of Perl style of comments (#) is not advisable.
  • PHP Code Tags - Always use <?php ?> to determine a PHP code, not the <? ?> shorthand. This is required for PHP compliance and also the most easiest way to include PHP code on differing operating systems and setups.
  • Variable Names -
    • Use lowercase letters.
    • Use underscore '_' as word separator.
    • Global variables should be prepended with 'g'.
    • Global constants ought to be all caps with '_' as the separator.
    • Static variables may be prepended with 's'.
  • Make Functions Reentrant - Functions should not keep static variables that prevent it from being reentrant.
  • Alignment of all Declaration Block - Block of declarations should be aligned.
  • Only a Statement Per Line - There ought to be only one statement per line unless if the statements are closely related.
  • Short Methods or Functions - Methods should limit themselves to a single page of code.


There could be considerably more points which should be considered while writing your PHP program. The overall intention should be to be consistent throughout the code and it will only be possible if you adhere to any coding standard.

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Predefined Variables.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.