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:


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:


function name()
   {
   function last()
      {
      //Do something
      }
   }

So, which do you use and why?

A all day… and for single-command if’s and loops, I use inline formatting, e.g.:


if (true) doSomething();

instead of:


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.

I prefer this style:


function doSomething()
{
   if(true)
   {
      // multiple actions
   }

   if(also_true) simpleSomething();
}

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.

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!

I use a different line for class and functions, but the same line on if, else, foreach parts.


function someFunc()
{
    if (condition) {
        ...
    }
}

In other words where possible, I try to follow the ZEND coding style

I use A, I can’t stand B. Its not that anything is wrong with B I just can’t it.

+1 lol

My full coding style:

MyClass.php


<?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);
    }
}
?>

This is a cop-out answer, but I follow the assigned coding standards of the project I’m working with.

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.

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.

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!

With Option B and suitable indenting, you tend to have a clear line of sight from the start of a section its closing brace.


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?


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.

A larger font works for me. :eye:

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… :wink:

True.
Although I didn’t see this particular option B before, that is with the opening and closing braces indented as well.

Here’s what I stick to, not only with PHP, but with JS, Java, and C#.

function fname(){
     if(something){
           //...
     }
     else{
          //...
     }
}

[FONT=“Georgia”]Exactly like B, including the comments too.

I do the same for Javascript.

[/FONT]

i also prefer this style

greets