PHP Coding Standards

Last Updated : May 28, 2021

Coding Standards are a set of rules and standards which programmers follow while developing web applications in PHP. Coding Standards help keep the code clean and understandable. Also, it makes the code review process very easy, and peer programmers can understand the code without any difficulties if they have to change anything in the existing code.

Why you should follow Coding Standards?

  • It helps to achieve high-quality code and set a standard for the software
  • Peer programmers can easily understand the code and modify them if needed without any difficulties
  • It always gives you enough information about the code; you don’t have to look for any documentation
  • It helps Company to stand among best in the industries
  • It makes the whole project as easily maintainable structure

In PHP, based on the different Frameworks and Libraries, developers follow different coding standards. For example: for Zend Framework or WordPress, developers following separate coding standards while developing the module. In PHP, developers generally follow PSR standards developed by a group of members from different frameworks and library communities.

PHP Framework Interop Group(PHP-FIG) established PSR-0, PSR-1, PSR-2, and PSR-4 coding standards. The PSR-1 and PSR-2 standards are for basic coding standards and widely used ones by many frameworks and libraries with tiny modifications. If we start to follow these two standards then our application will be in good shape for the start. We will learn about some common and basic coding standards to follow in our web application below.

Basic PHP Coding Standards

Please find below the basic coding standards and best practices to follow when coding using the PHP language.

PHP Base Tag

We must use standard open <?php and close ?> tags, not short tags<??>.

Indentation and Line length

It’s better to follow 4 space indentations in the code block, and the maximum line length should be around 85-100 characters, but many developers and editors follow a line length of 85.


To comment on a single code line, we should use double-slash //, and for a multi-line code block, /* */ are regularly used, and it’s considered the best standards in PHP.


In PHP, while creating Functions, Classes, and Conditional statements, the necessary spaces, and line break should be used between open/close parentheses and open/close curly braces. Every open brace should have its own closing brace without missing and must be aligned with proper indentation. It improves better code readability and understandability. Please find below the example,

// this is single line comment example
$fruit = 'Apple';
if ($fruit == 'Apple') {
   echo "Its Apple!"; 

Variable Names

We already discussed Variable naming conventions in this chapter, please find below few important rules for creating variable names.

  • All variable names must start with a $ sign; they will not be considered variables without it.
  • All variable’s names should start with alpha-numeric(a-z, A-Z) characters and underscore(_).
  • A Variable name should not start with numbers or special characters.

Most of these rules apply to Constant with dollar sign and Function names. Based on the Framework, developers will use snake_case type variable names and CamelCase variable names.

Spaces and Commas

To make a code look better, we have to use spaces and commas(,) whenever is necessary and possible. For example, while assigning value to a variable it’s better, we should use some spaces between the variable name and value like this $variable_name = value instead of $variable_name=value. And, after every comma, if we use one space then it’s considered as best practice. For example, when we provide multiple parameters in the function, the best practice to use space after each parameter ends. Please find below some best practices,

$first_name = 'John';
$first_name .= 'Doe'; //space after assignment

$array = array();
$a = true;
if ($a) {
  echo "Yes";

//function parameters
function add($number_1, $number_2) {
   return $number_1+$number_2;

Depending upon the Framework, white-spaces may be given before and after parentheses.

Best Practices and Standards

Please find below the consolidated best practices and Do’s/Don’ts while coding.

  • Must use spaces for Indentation, not tab.
  • Use space after every parameter in the function.
  • Wrap the code line after 80-100 characters.
  • Don’t use a short PHP tag; use full open/close tags.
  • Use snake_case, CamelCase for Variables, Functions, and Classes.
  • Use uppercase for constants, boolean(true, false), and null values.
  • Use single-line and multi-line comments properly, do not use multi-line comments for single line and vice-versa.
  • Use built-in or custom Exceptions always in the code.
  • Don’t use functions inside any loops; it will cost computation when records increase.
  • Use numeric, associative, and multidimensions array effectively based on the requirements.
  • Always use error_reporting(E_ALL) while developing and avoid notices, warnings, and errors in any case.
  • Use the latest version of PHP to avoid security breaches and always be updated.
  • Structure the project properly like tree view, do not just put all the files inside the main directory/folder.
  • Try different PHP Frameworks and get to know the MVC patterns.
  • Start creating Unit Tests for your code block.
  • Follow different blogs/websites to know the latest concepts about web technology and PHP programming language update.

Useful Tools

With recent innovative Editors/IDEs, we can achieve basic coding standards and correct syntax errors. Please find below few tools which can make our work easy.

In this tutorial, we discussed PHP Coding Standards and their best practices. We will use these standards throughout our tutorial series and their examples.