SitePoint Sponsor

User Tag List

View Poll Results: What format do you use for coding?

Voters
54. You may not vote on this poll
  • A: Opening brace on same line

    34 62.96%
  • B: Opening brace indented on new line

    9 16.67%
  • C: Other

    11 20.37%
Page 1 of 3 123 LastLast
Results 1 to 25 of 62
  1. #1
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    POLL: How do you format your php?

    I posted this on another php forum recently and would like to see the results here on sitepoint.

    I've been a delphi coder for years and when switching to php I noticed that the format by many coders is different. In delphi anything seems to go but in php I noticed people use the following format a lot:

    A:
    PHP Code:
    function name(){
      function 
    last(){
      
    //Do something
    }

    I've always found that rather hard to read (EG It's hard to identify which closing brace belongs to which opening brace when you have a lot of them).

    I've always coded my code like this:

    B:
    PHP Code:
    function name()
       {
       function 
    last()
          {
          
    //Do something
          
    }
       } 
    So, which do you use and why?
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A all day... and for single-command if's and loops, I use inline formatting, e.g.:

    Code:
    if (true) doSomething();
    instead of:

    Code:
    if (true)
        doSomething();
    I just find it to be more efficient for navigation of the code, because it reduces total lines of code by about 10-30% - that's significant.

  3. #3
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I prefer this style:
    Code PHP:
    function doSomething()
    {
       if(true)
       {
          // multiple actions
       }
     
       if(also_true) simpleSomething();
    }
    zalucius

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Option C: I do what zalucius does.

    Funnily enough, in Javascript I use A. Somehow it feels more natural in JS, but PHP it feels weird.
    Go figure.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In fact, I'm the opposite to you Scallio. I use A for PHP, but C for everything else. I don't know why, I just... do!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I use a different line for class and functions, but the same line on if, else, foreach parts.

    Code php:
    function someFunc()
    {
        if (condition) {
            ...
        }
    }

    In other words where possible, I try to follow the ZEND coding style
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I use A, I can't stand B. Its not that anything is wrong with B I just can't it.
    The only code I hate more than my own is everyone else's.

  8. #8
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    i use a, i can't stand b. Its not that anything is wrong with b i just can't it.
    +1 lol

  9. #9
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My full coding style:

    MyClass.php
    PHP Code:
    <?php
    /**
     * Class Description
     */
    class MyClass extends YourClass {
        const 
    FOO "foo";
        const 
    BAR "bar";

        private 
    $variableName1;
        protected 
    $variableName2;
        public 
    $variableName3;
        public 
    $children;
        
        
    /**
         * Constructor
         * @param $variableName1 int Description
         * @param $variableName2 int Description
         */
        
    public function __construct($variableName1$variableName2 2) {
            
    $this->variableName1 $variableName1;
            
    $this->variableName2 $variableName2;
            
    $this->children = new array();
        }

        
    /**
         * This is my function description
         * @return int Whatever
         */
        
    protected function internalFunction() {
            
    // Do Something Here
        
    }

        
    /**
         * Adds a child item to the class
         * @param $child variant Whatever
         */
        
    public function addChild($child) {
            
    array_push($this->children$child);
        }

        
    /**
         * Renders the output and prints to the page
         */
        
    public function render() {

            
    // Start with some basic output
            
    $output = <<<HTML

                <div>
                    <p>This is output attempt number 
    {$this->variableName1} </p>
                </div>
    HTML;

            
    // Print our foo/bar status
            
    switch ($this->variableName2) {
                case 
    self::FOO:
                    
    $output .= "Some foo stuff";
                    break;
                case 
    self::BAR:
                    
    $output .- "Some bar stuff";
                    break;
                default:
                    
    // No output
            
    }

            
    // Try to output children
            
    try {
                
    // Loop children and print each one
                
    foreach ($this->children as $child) {
                    if (isset(
    $child)) $output .= $child;
                }
            } catch (
    Exception $e) {
                
    // Output error message
                
    $output .= "oops!!! error!";
            }

            
    // Print output
            
    print($output);
        }
    }
    ?>

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    This is a cop-out answer, but I follow the assigned coding standards of the project I'm working with.
    Salathe
    Software Developer and PHP Manual Author.

  11. #11
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    My coding style matches the Zend formatting guidelines pretty much exactly, by coincidence. The only difference is that I use an indent of 2, not 4.

  12. #12
    SitePoint Member Houdas's Avatar
    Join Date
    Feb 2005
    Location
    Czech Republic
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends... on single-line if(), while() etc. I put brace on the same line. On switch(), function(), class etc. I put brace on a new line.

  13. #13
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow!

    I'm quite surprised because on the other site I ran this poll option B was slightly more preferred than option A.

    I've always preferred B due to the ease of which you can see which code block you're inside of and where the opening and closing braces are etc. The inline option A has always annoyed me as I've never been able to understand it properly.

    Keep voting!
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by tangoforce View Post
    I've always preferred B due to the ease of which you can see which code block you're inside of and where the opening and closing braces are etc. The inline option A has always annoyed me as I've never been able to understand it properly.
    With Option B and suitable indenting, you tend to have a clear line of sight from the start of a section its closing brace.

    Code php:
    for ($i = 0; $i < $someLength; $i++) {
        if ($i < $someValue) {
            doSomething();
        } else if ($i < $someOtherValue) {
            doSomethingElse();
        } else {
            doSomeOther();
        }
    }

    Option A tends to result in the code taking up too much vertical space. You're good if you're paid by the line of code, but going from 9 lines to 15 lines for the same code, what does that achieve?

    Code php:
    for ($i = 0; $i < $someLength; $i++)
    {
        if ($i < $someValue)
        {
            doSomething();
        }
        else if ($i < $someOtherValue)
        {
            doSomethingElse();
        }
        else
        {
            doSomeOther();
        }
    }


    I am tempted to inquire whether people who prefer the above code might require an eye-glass for proper reading.

    But to be more politic about it, we all have our reasons as to why we prefer our own ways.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I am tempted to inquire whether people who prefer the above code might require an eye-glass for proper reading.
    A larger font works for me.
    Salathe
    Software Developer and PHP Manual Author.

  16. #16
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tangoforce View Post
    Wow!
    I'm quite surprised because on the other site I ran this poll option B was slightly more preferred than option A.
    That's because you inadvertently exploited the other forums' post-parsing algorithm with just the right combination of code, memory, and timing necessary to throw one of the data mining bots from China into a Kafkaesque infinite voting loop...

  17. #17
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    we all have our reasons as to why we prefer our own ways.
    True.
    Although I didn't see this particular option B before, that is with the opening and closing braces indented as well.

  18. #18
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Here's what I stick to, not only with PHP, but with JS, Java, and C#.

    Code PHP:
    function fname(){
         if(something){
               //...
         }
         else{
              //...
         }
    }
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  19. #19
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Exactly like B, including the comments too.

    I do the same for Javascript.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  20. #20
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zalucius View Post
    I prefer this style:
    Code PHP:
    function doSomething()
    {
       if(true)
       {
          // multiple actions
       }
     
       if(also_true) simpleSomething();
    }
    i also prefer this style

    greets

  21. #21
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,832
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by zalucius View Post
    I prefer this style:
    Code PHP:
    function doSomething()
    {
       if(true)
       {
          // multiple actions
       }
     
       if(also_true) simpleSomething();
    }
    That's what I do. There is no point taking up 4 lines for a simple if statement if you can do it on one line.

    And the braces should be on their own lines and indented similarly so the opening and closing brace are the same distance from the left. This style is the easiest to read.

  22. #22
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    There is no point taking up 4 lines for a simple if statement if you can do it on one line.
    Quote Originally Posted by cheesedude View Post
    And the braces should be on their own lines
    These 2 statements completely oppose each other!

    I put the opening brace on the same line as the control statement, with a space in between, and the closing brace on its own line, usually with a comment afterwards to say what it is closing (Not really necessary, but it's a habit!).

    Everything inside the control block is then indented 2 spaces.

    eg:

    PHP Code:
    if (somecondition) {
      
    someStatements();
    }
    //if 
    I use the same rule for all control statements, functions, classes, everything. I don't understand the reason for treating classes & functions differently to any other type of block.

    I also never use the 1 line if statements, they always have braces for consistency and ease of seeing what belongs to what. Sometimes it's easy to miss the fact that an 'else' does or doesn't have a brace after it, and then lose track of whether only 1 thing is executed or more than one etc..

    It's really easy to see blocks due to the indentation - so some lines are used for simple if statements that could be done on one line, for the sake of readability, but no readability is lost by putting opening braces on the same lines as control statements due to the indentation.

  23. #23
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Stormrider View Post
    I don't understand the reason for treating classes & functions differently to any other type of block.
    That style stems from Kernighan and Ritchie's style who invented the C programming language.
    A minor variation of that is the One True Brace Style (1TBS), where single statement blocks are enclosed in braces too, which is the style that many people use.

    Commonly with PHP, people choose a style guide to stick to such as ZEND or PEAR, which also applies the 1TBS. Any variations from that style guide tend to be attacked by other team members, so style guides remain influential for a very long time.

    If you're not working in a team, then it is of course much easier to divert from such standards to arrive at your own unique style.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  24. #24
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I know the roots of it and about all the different formatting styles, but I still don't know why they are treated differently!

  25. #25
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Stormrider View Post
    I know the roots of it and about all the different formatting styles, but I still don't know why they are treated differently!
    Go back and have a read then about Kernighan and Ritchie's style for C code.

    in the original C language, argument types needed to be declared on the subsequent line
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •