PHP Coding Standards and Guidelines

Hello folks! welcome back to another section of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP Coding Standards.

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

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

  • Simplicity and clearity which is achieved from consistent coding helps avoiding some common mistakes.
  • If you revise your code after some time then it becomes very easy to understand that code.
  • Your peer programmers have to understand the code you produce. 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 & do not use any tab because various machines make use of different settings for tab. It is adviced to keep lines at about 75-85 characters long for better code readability.
  • Control Structures - These includes if, for, while, switch, etc. The control statements should have at least 1 space between the control keyword and opening parentheses, to differentiate them both from function calls. You're hardly encouraged to always make use of curly braces even in cases where optional.


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

You can write switch statement as follows -

switch (condition) {
   case 1:
   case 2:


  • Function Calls - Functions in PHP have to be called with no spaces in the middle of the function name, opening parenthesis and the opening parameter; spaces between commas & each parameter, and no space between the last parameter, the closing parenthesis and semicolon.


The following below is a simple example -

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

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


The following below is a simple example -

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

  • Comments - You can use C style comments (/* */) and standard C++ comments (//) in your code. The use of Perl style of comments (#) is not advisable.
  • PHP Code Tags - Make sure you always use <?php ?> to determine a PHP code, not the <? ?> shorthand. This is needed for PHP compliance and is 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 - The methods should limit themselves to a single page of code.

There may be many more points that 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 i 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.