Jump to content

Archived

This topic is now archived and is closed to further replies.

redneonrt

PHP Formatting

Recommended Posts

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

Share this post


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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
I agree with wildteen. Additionally I prefer to write [tt]else if[/tt] instead of [tt]elseif[/tt] like some people do. And instead of [code]echo "Your name is ".$name."!";[/code] I prefer to use [code]echo "Your name is {$name}!";[/code]

Share this post


Link to post
Share on other sites
many different "styles." the key is consistency throughout the code.  And don't be afraid to use comments.

Share this post


Link to post
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]

Share this post


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

Share this post


Link to post
Share on other sites

×

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.