views:

328

answers:

7

I'm curious to know, how many spaces of indentation do you prefer in PHP code?

function one()
{
 $one;
 function space()
 {
  $space;
 }
}

function two()
{
  $two;
  function spaces()
  {
    $spaces;
  }
}

function three()
{
   $three;
   function spaces()
   {
      $spaces;
   }
}

function four()
{
    $four;
    function spaces()
    {
        $spaces;
    }
}

Let's not make multiple answers for same indentation, but use the +1 for answers that fit your preferences.

+11  A: 

One tab per level. The only correct way!

Greg
Aka the python way!
Enrico Carlesso
+1  A: 

2 spaces per level. It looks neat in Notepad.

function testfunc($x){
  if($x & 1){
    return $x / 2;
  }
  return $x;
}
thephpdeveloper
+1  A: 

I use four spaces like the PEAR Coding Standards suggest.

Gumbo
+4  A: 

I use what's defined by the coding standards of the Framework I use for the project I'm working on :

  • For instance, if I'm working on a Drupal based project, it's two spaces.
  • If working on a project based on Zend Framework, it's four spaces.
  • By default, if nothing else has been defined, I tend to use PEAR coding standard, which are well know and well accepted in the PHP community.

This way, everything in the project is consistent : it makes things easier for new people who start working on the project after it started.

And, of course, this apply to other rules of formating too, like where to put the {}, for example.

Pascal MARTIN
Use spaces becouse of the version control issues, and set up your editor to write 4 spaces instead of a tab.
erenon
A: 

I use tabs, the various advanced text editors available for coders allow you to modify tab spaces at a later date.

I don't see why anyone would want to press space 4 times to produce an indent, it sounds like such a nuisance. I don't really care about what the proposed correct method is, I have something which suits me and is easier.

It's really not a big deal, my advice is to do what you feel. In the end, only you can decide.

meridimus
Most decent editors will turn a <tab> into 4 (or 2, or 8, or whatever) spaces.
mipadi
A: 

I've seen it as both way, whether it be spaces or tab. However personally I prefer 1 tab for my spacing as it's quick, easy and clean, not to mention it requires no thought as to how many times I've hit the space bar.

GameGamer43
A: 

It would be a good idea for you to choose a Standard that phpcodesniffer supports. That way you can run the phpcs tool on your code to scan for any deviations from whichever standard you choose.

Personally I favour the PEAR Coding Standards but you might prefer a different one.

kguest