How to Write Standardized PHP Code?

For PHP, there are many standards and coding style recommendations that you should look into when writing code. It simplifies collaboration on code and makes it more readable once you get used to a particular style.

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

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

Many open source projects also extend the 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.

See Also

GitHub OctocatFound 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 the contributors.