SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Formatting PHP code

  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?
    Other?

    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?

    Thanks,



    Debbie

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,539
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    Personally for me I'm used to a 4 space tab indent as its easier for myself and others to read my code
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  5. #5
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,609
    Mentioned
    76 Post(s)
    Tagged
    4 Thread(s)
    I use 2 space tabs. 4 spaces gives too much white space for my liking

  6. #6
    SitePoint Evangelist SitePoint Award Recipient captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    418
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    4 spaces. I no longer remember why I switched from tabs to spaces.
    Denny Schlesinger
    web services

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    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.

  8. #8
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    4 spaces.

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

  9. #9
    From space with love SitePoint Award Recipient SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,271
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    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:

    eg:

    PHP Code:
    if ($something === $somethingelse) {
    // do this
    }
    //rest of code 
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  10. #10
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    3,659
    Mentioned
    21 Post(s)
    Tagged
    1 Thread(s)
    1 tab.

    Code:
    if(myString=="something"){
        doSomething();
    }
    else{
        doSomethingElse();
    }
    I've always done it across C, Java, CSS, HTML, C#, and PHP. Partly habit, and partly readability.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  11. #11
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    8,993
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I use 2 spaces. I am also very liberal with my space bar and enter key.

    Code php:
    <?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   [URL]http://[/URL][vendor]/legal/oss/license BSD License
     * @license   [URL]http://[/URL][vendor]/legal/pro/license Proprietary License
     * @link      [URL]http://[/URL][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
        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; }
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    3,845
    Mentioned
    11 Post(s)
    Tagged
    3 Thread(s)
    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.

  13. #13
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    8,993
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    ...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.
    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.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •