Formatting PHP code

How do you format your PHP code…

Do you use Tabs or Spaces to indent?

And by how many positions do you indents?

1 position?
2 positions?
4 positions?

I think a standard line is 80 characters, so I’m starting to wonder if using a 4-position indent is too much?!

(It is easier to read than a 2-position indent, but also seems to waste space?!)

I am using NetBeans as an IDE, and it allows you to change whether us use Tabs or Spaces to indent, and by how much you indent.

What do you do and why?



You can get around those issues by having an editor at lets you configure how it interprets the tab key. I use Komodo which lets me set a global setting, per-language setting, and even per-file setting.

1 tab unless I run into something that uses something different than I’ll adapt so the next person looking at it doesn’t have to deal with inconsistency.

In regards to the why, I’m not really sure, its just what I’ve been using since I’ve began programming and hasn’t been an issue. Although, hitting tab once is less prone to error than multiple space bar taps or tab taps. Though those types of error won’t result in breaking code just formatting, so yeah.

I use 2 spaces. I am also very liberal with my space bar and enter key.

<?php namespace [Vendor]\\[Project]\\[Package];
 * Short Description...
 * Long Description can span many lines and contain examples or any other
 * details. Most cases this portion will be blank.
 * @copyright 2009 [Author|Company]
 * @license   http://[vendor]/legal/oss/license BSD License
 * @license   http://[vendor]/legal/pro/license Proprietary License
 * @link      http://[project].[vendor]/api/[package]/[component]
 * @category  [project]
 * @package   [package]
 * @version   $Id$
# 80 char range
use Some\\OtherA\\Name\\Space as AliasA,
    Some\\OtherB\\Name\\Space as AliasB;
 * Description and example of the inner workings of this compnent.
 * @category [project]
 * @package [package]
class ComponentCamelCase extends AliasA implements AliasB
    CONST_ONE   = 1,
    CONST_TWO   = 2,
    CONST_THREE = 3;
  protected // Always protected!
    $varOne   = 'default',
    $varTwo   = 'default',
    $varThree = 'default';
   * Short Description...
   * Long Description...
   * @param string  $a  description...
   * @param integer $b  description...
   * @return void
   * @throws UnknownException, KnownException
  public function camelCase ( $a, $b )
    if ( expression ) {
      // multi-line action...
      // lots of code...
    } elseif ( expression ) {
      // more multi-line actions...
      // more lots of code...
    } else
      // fallback action...
    // or, for single line
    if ( expression ) // action...
      // or put it under...
    $arr = array( 'key' => 'value' );
    $arr['key'] or $arr[ $key ];
    # mixed ( string $a, mixed $b ) + array $global
    # throw UnknownException, KnownException
    $call = function ( $a, $b ) use ( $global )
        // action...
    return $this; // Always return $this unless it must return something else.
  protected function specialMethod () { return $this; }

I have the bulk of code indented by six spaces, then each block is indented by 4 spaces, in the editor that I use when you have any { } blocks it indents then, I prefer to have the opening { on the same line as the preceding code:


if ($something === $somethingelse) {
// do this
//rest of code

1 tab.


I’ve always done it across C, Java, CSS, HTML, C#, and PHP. Partly habit, and partly readability.

I just use whatever the editor in question uses… by default for the current one i’m using, that’s an additional indent of 2 spaces per depth of braces.

I use 2 space tabs. 4 spaces gives too much white space for my liking :slight_smile:

Personally for me I’m used to a 4 space tab indent as its easier for myself and others to read my code

4 spaces.

That was a coding convention at one of my employers, and it’s stuck ever since.

I still use vim, and that is set to translate tab to 4 spaces.

I rarely share code, but if I do I run it through a beautifier first because I am pretty slapdash about what gets indented for my own selfish reasons.

I have been ridiculously inconsistent over the years and tend to adopt the style prevalent in the code base/libs I am working on, rather than bite the bullet and re-arrange the whole thing. Not in the tab=4spaces thing, but whether they are 1 or 2 tab indenteded.

When starting from scratch, I am much better now.

The same goes for commenting I must say.

4 spaces. I no longer remember why I switched from tabs to spaces. :slight_smile:

Currently, I’m using Tabs set at 2 spaces.

The reason for this, rather than having a pre-set indent at 2 spaces, is that it still allows other devs, or even myself, to specify the width of the indent.