PHP style guide

PHP code convention

All programming languages have one or two code conventions that are followed by all programmers using the language. But not PHP.
PHP has a lot of conventions so no developer follows any of them and writes the code as he wants. Also PHP has a lot of frameworks and every one of them has its own code convention.

So the problem is: How to choose a code convention?

And there are 2 ways: Use an existing one or follow your own.
It's nice to follow your own but best to use existing.
My decision on how to choose one was to put on a list the main topics and try to explain for myself why I should use this or that. After having answers on these main questions I chose an existing code convention which matches my preferences and adds all the little stuff that there's no need to explain.
The important word here is "explain". Because no existing convention says "why".

I've made these conclusions after about 10 years of coding, the last 5 of which - almost every day. If you don't like them - you are free to make your own. :)

Tabs vs spaces

If you haven't gone through this discussion you should know that this is the greatest programming question ever.

I say you should use tabs and most importantly here's why:

  1. Customizable - When working in a team some of the people prefer 4 spaces indentation, others - 2. If using tabs for indentation everyone can set the tab representation as he wants. Also when you have to check the code you have written years ago and your style has changed - you can customize it easy.
  2. Tab is made for indentation and therefore should be used for indentation.
  3. Using tabs instead of spaces you reduce the file size by 15%. Not believing - try it.

Curly braces placement

Curly braces should always be put on a separate line all by themselves. Always.

The reason behind this is that PHP is character-rich language - you can use all types of braces, all symbols you can write with the shift key and so on and so on. So if you put the opening braces on the same line and then start the body on the next line sometimes it becomes hard to read and understand what is going on. By putting the opening braces on the next line alone the body's scope is perfectly clear.
And all these braces should be used this way because this is the same character and there's absolutely no sense in writing it differently.


Should be written uppercase. The difference between the constants that should be written uppercase and these three is minimal. Also these are all used to compare with variables ($something), constants (CHRISTMAS), numbers (105) that start with capital character.


Files line ending: LF.

?> Close tag: Always missing.


I came up to comparing several of the famous existing code conventions and the conventions that the most famous frameworks use. The ones I compared were:

  1. Zend
  2. PEAR
  3. PSR
  4. Laravel
  5. Phalcon
  6. Symfony
  7. CodeIgniter
  8. Yii
  9. CakePHP
  10. Kohana
  11. FuelPHP
  12. WordPress
  13. Drupal
  14. Joomla
  15. PrestaShop

Let's compare the three topics that I wrote about early. For the braces let's get the control statements braces.

Name: Tabs/Spaces - Same/Next - Lower/Upper

The correct answers should be: Tabs - Next - Upper

  1. Zend: 4 spaces - Same - Lower
  2. PEAR: 4 spaces - Same - Lower
  3. PSR: 4 spaces - Same - Lower
  4. Laravel: Tabs - Next - Lower
  5. Phalcon: 4 spaces - Same - Lower
  6. Symfony: 4 spaces - Same - Lower
  7. CodeIgniter: Tabs - Next - Upper
  8. Yii: Tabs - Next - Lower
  9. CakePHP: Tabs - Next - Lower
  10. Kohana: Tabs - Next - Upper
  11. FuelPHP: Tabs - Next - Lower
  12. WordPress: Tabs - Same - Lower
  13. Drupal: 2 spaces - Same - Upper
  14. Joomla: 4 spaces - Same - Lower
  15. PrestaShop: Tabs - Next - Lower

So what we see here is that the old ones follow the PSR while the new ones use tabs for indenting and put the braces on the next line.

So I have to choose CodeIgniter or Kohana. After looking through the full conventions of these two frameworks I chose CodeIgniter's.

Here's a link to it:

CodeIgniter FTW

If you wonder what convention you should use - take a look at the code you've written through your career. If you understand everything fast - that's a good sign. If it is a lot of effort to you - that's bad.

So whatever you choose - make sure you won't swear when you have to read it several years later.

Lucky giraffe