Jump to content


Photo

PHP Formatting


  • Please log in to reply
9 replies to this topic

#1 redneonrt

redneonrt
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 September 2006 - 04:27 PM

I was looking for something to show me how to write and format PHP code correctly, things like when to tab in and proper spacing ect.

Thanks for looking

#2 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 06 September 2006 - 05:08 PM

I set my editor to replace tab keystrokes with two spaces.
The simple rule I use is to press tab on a new line after any opening curly brace and to delete two spaces when I type the closing curly brace.  Always use curly braces, even when they're optional.  And never let a line go past 80 characters.  Use lots of comments, avoiding the block comment (/* */) syntax so you can comment out blocks later if need be.

// Define consts
define('CONST1', 'VAL01');
define('CONST2', 'VAL02');

// This class does blah blah yadda yadda
// Use for asdf qwerty
class MyClass extends MyBase{
  function MyClass(){
    // comment
    $var = $val;
    if(isset($something)){
      // comment
      // comment
      $hello = $world;
    }
    $this->ReallyLongVarName = SomeOtherClass::ReallyLongFuncName( $val01,
                                                                                                $val02,
                                                                                                $val03 );
    $this->arr = Array(
                              $ReallyLongArrayValue01,
                              AnotherBetterClass::WithAnotherLongFunc(),
                            );
    return;
  }
}
$val02 and $val03 in the function call within SomeOtherClass should be directly below $val01, my browser isn't placing it correctly when I view my post though.

Last note, there is no right or wrong way.  But there are best and worst ways.  The best ways are so that you can come back to the same code written a few months ago, or not even your code, and still figure out what it does.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 September 2006 - 05:17 PM

Yeah I agree with roopurt18, however I prefer 4 spaces rather than two for a tab, and i like my curly braces to be on thier own line, eg:
// Define consts
define('CONST1', 'VAL01');
define('CONST2', 'VAL02');

// This class does blah blah yadda yadda
// Use for asdf qwerty
class MyClass extends MyBase {

    function MyClass()
    {
        // comment
        $var = $val;

        if(isset($something))
        {
            // comment
            // comment
            $hello = $world;
        }

        return true;
    }
}
IMO it makes the code more readable, and you can tell where your code blocks start and end, where as if the opening curly brace isn't on its own you can only see where the code block ends. It also makes it easier to pair up the curly braces too.

#4 redneonrt

redneonrt
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 September 2006 - 05:21 PM

So its really up to the person writing it? Whatever makes it easiest for them to read? or are there a set guidline to follow?

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 September 2006 - 05:34 PM

There is no set guidelines to how PHP code should be formated AFAIK. Its mainly down to the personal preference of the programmer. However if developing a script with a group of other programmers and they all have a different styles of coding, then there should be guidelines set out for developers to follow when programming the web application, to help keep the code clear and readable.

As long as the code is clear and readable to anyone that sees it then it doesnt matter what style you format your code.

#6 redneonrt

redneonrt
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 06 September 2006 - 05:36 PM

OK thanks for everyones response

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 06 September 2006 - 05:41 PM

I agree with wildteen. Additionally I prefer to write else if instead of elseif like some people do. And instead of
echo "Your name is ".$name."!";
I prefer to use
echo "Your name is {$name}!";


#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 September 2006 - 05:46 PM

many different "styles." the key is consistency throughout the code.  And don't be afraid to use comments.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 06 September 2006 - 06:06 PM

And don't be afraid to use comments.


Don't comment the obvious, however. E.g.:
echo $username; // echo'es the username
or
if(is_array($var)) // check if $var is an array


#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 06 September 2006 - 07:34 PM

A guideline for comments

Ask yourself "If I  come back to this piece of code in six months time, or someone else looks at it, will we immediately understand what it's doing and how it works?"

If the answer is "No", add comments
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users