views:

99

answers:

4

I'm a beginner PHP coder, recently I've been told I indent my code not correctly. They say this is wrong:

    if($something) {
        do_something();
        }
    else {
        something_more();
        and_more();
        }

While this is right?

    if($something) {
        do_something();
    } else {
        something_more();
        and_more();
    }

Really? I am willing to become opensource coder in nearest future so that's why I'm asking how to write code in a good way.

+5  A: 

Both ways will run just fine. Whichever is more comfortable for you and your team would be the way to go. I personally favor the second example, or something alone these lines:

if ($something)
{
  do_something();
}
else
{
  do_something_else();
  pet_the_ponies();
}

There's no real right answer to this.

Jonathan Sampson
Thank you, it explains things rather good.
+3  A: 

In many cases, coding standards say, "match the style of the file you're editing," ie. don't change conventions if the file already has a given brace and whitespace style.

I would look over the coding standards for some popular projects to get a feel for what some patch wranglers expect:

&c.

Adam Backstrom
Thanks for links!
+2  A: 

There are many Coding Standards. @Adam gave You two links, I give You another: Zend PHP Coding Standard You can choose, what You want, but You should choose most popular standard, I think.

You can use even Code Conventions for the Java It doesn't really matter.

Michał Mech
+1  A: 

CodeIgniters user guide got some great tips on how to write your code: http://codeigniter.com/user_guide/general/styleguide.html

Some are really good, but some you'll have to mix and match to get your own good style. But as said before me you should stick to one style and if you're jumping into another project you should follow the style already set. There's no right or wrong in how to style you're code, it's mostly so that you don't get lost when going back to the code after a few months.

Dom