开发者

PHP Line Indentation [closed]

开发者 https://www.devze.com 2022-12-08 15:43 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answ开发者_如何学Goered with facts and citati
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answ开发者_如何学Goered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question

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.


One tab per level. The only correct way!


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.


After more years of software dev, I now follow the PSR-2 standard of four spaces per level.

2 spaces per level. It looks neat in Notepad.

function testfunc($x){
  if($x & 1){
    return $x / 2;
  }
  return $x;
}


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.


I use four spaces like the PEAR Coding Standards suggest.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消