Jump to content

PHP Formatting


redneonrt

Recommended Posts

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.

[code]
// 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;
  }
}
[/code]
$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.
Link to comment
Share on other sites

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:
[code=php:0]// 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;
    }
}[/code]
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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=Crayon Violent link=topic=107129.msg429390#msg429390 date=1157564770]
And don't be afraid to use comments.
[/quote]

Don't comment the obvious, however. E.g.: [code]echo $username; // echo'es the username[/code] or [code]if(is_array($var)) // check if $var is an array[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.