PHP coding style

Coding style (programming style) is a set of rules and guidelines how to indent code, where to put opening curly bracket, use camelCase, StudlyCase, or under_score conventions and similar.

PHP allows a lot of code styles, which is good and very friendly to start with. However, as soon as you write more PHP code or when collaborating on PHP code with others, different coding styles will get in a way very soon and code will be less readable.

For PHP, there are already existing standards and coding style recommendations that you should look into when writing code. Adopting a consistent coding style will simplify collaboration on code and make it more readable.

One of the most adopted standards recommendations in PHP is PHP FIG, which recommends coding style via two of its so called PSRs (PHP Standard Recommendation):

  • PSR-1 - Basic Coding Standard
  • PSR-2 - Coding Style Guide

Many open source projects also extend above PSRs with their own coding style guides. For example:

Many advanced PHP IDEs and editors also offer code refactoring via plugins and extensions. With predefined common coding styles such as PSR-1 and PSR-2, with refactoring code, automatic code generation, autocomplete and similar things, consistently writing standardized PHP code can be done with ease.

The more you write code, the more you will understand the importance in using common style guides. Especially when working with source code control (eg, Git, coworkers), or just to be consistent in general.

Additional coding style guidelines

These are some further best practices that are either not defined in the PSR coding style yet, or are recommended by the PHP documentation.


Constants should be defined with uppercase letters and underscore separators:


const FOO = 'value';
const FOO_BAR = 'value';
  • Omit constant names such as __FOO__, because PHP might one day use such contant name internally.
  • Use only letters and underscores. Omit all other characters, such as numbers, for simplicity and readability.

Other coding styles

Some of the PHP open source projects and their coding styles:

See also

Found a typo? Something wrong with this content?

Just fork and edit it.

Content of this work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. Code snippets in examples are published under the CC0 1.0 Universal (CC0 1.0). Thanks to all contributors.