Coding Standard for PHP

To do best Coding Standard for PHP, each company has its own coding standard. Coding standards are needed because there may be a lot of developers working on different modules. If they all come up with their own standards, the source code will be hard to manage and keep up to date in the future.

Here are a few reasons why you should use coding specs:

  • Your fellow programmers must be able to read the code you write. A coding standard tells everyone on the team how to figure out the code.
  • By keeping your code simple and clear, you can avoid making common mistakes.
  • When you go back and look at your code after some time, it will be easy to understand.
  • It’s standard in the industry to follow a certain standard to make software better.

When writing PHP code, there are a few rules that can be followed.

Indenting and Line Length: Use a 4-space indent and don’t use any tabs because tabs are set differently on different computers. It is best to keep lines between 75 and 85 characters long so that the code is easier to read.

Control structures: Control structures are things like if, for, while, switch, and so on. To tell them apart from function calls, there should be one space between the control keyword and the opening parenthesis. You are strongly encouraged to always use curly braces, even when the rules say you don’t have to.

Coding Standard for PHP Examples

if (condition) {
code to exicute;
}elseif (condition) {
code to exicute;
}else {
default defaultaction;

//You can write it with switch statements as follows −

switch (condition) {
code to exicute;

code to exicute;


Function Calls: When calling a function, there should be no space between the name of the function, the opening parenthesis, and the first parameter. There should be a space between each comma and each parameter, but there should be no space between the last parameter, the closing parenthesis, and the semicolon. Here’s what I mean:

$varible = foo($val1, $val2, $val3);

Function Definitions:  A block of statements that can be used over and over again in a programme is called a function. When a page loads, a function won’t run on its own. By making a call to a function, the function will be run.

function Name_Function($val1, $val2 = ”) {
if (condition) {
return $val3;

Comments You can use either C style comments (/* */) or standard C++ comments (/). It is not a good idea to use comments in the Perl/shell style (#).

Tags: PHP Code Always use<?php    ?> instead of the shorthand<? ?> to separate PHP code. This is required by PHP and is also the easiest way to include PHP code on different operating systems and in different ways.

Variable Name rules −

  • Use only small letters.
  • Use ‘_’ to separate the words.
  • The letter ‘g’ should come before all global variables.
  • All caps and a ‘_’ should be used to separate global constants.
  • You can put an’s’ in front of static variables.
  • Make functions reentrant. Functions shouldn’t keep static variables that stop them from being reentrant.

Declaration Blocks Should Align: Declaration blocks should be aligned.

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

Short Methods or Functions: Methods should not be more than one page long.

There may be many more things to think about when writing your PHP programme. The main goal of coding should be to be consistent all the way through, and you can only do that if you follow a coding standard. If you want something different, you can come up with your own standard.

People also search

Leave a Comment

Scroll to Top