PHP Syntax Formatting - Page 4
Page 4 of 6 FirstFirst ... 23456 LastLast
Results 46 to 60 of 85

Thread: PHP Syntax Formatting

  1. #46
    To code, or not to code? jkurrle's Avatar
    Join Date
    Jul 2004
    Location
    Gainesville, VA
    Posts
    524
    One thing I like to do is especially mark out new sections with comment lines. While this may seem obvious, when you start writing code into the thousands of lines, you need sections to really stand out. So, at the beginning and end of major sections, I'll comment something like this:

    PHP Code:
    /*** Some_New_Section ***/
    // some code here...
    /*** End Some_New_Section ***/ 
    The larger or more important the section, the more asterisks I use to delineate it. That way, when I'm quickly paging through the code, I can immediately see where I'm at.

    Another trick I sometimes do, when I have a lot of MySQL queries, is to put a unique marker in the die statement, like this:

    PHP Code:
    $result=mysql($query)
      or die(
    "Internal Error XQ93: MySQL Query failed with this message: ".mysql_error()); 
    That way, I can do a quick search on "XQ93" and get immediately to the query in question...
    99 little bugs in the code, 99 bugs in the code,
    fix one bug, compile it again...
    101 little bugs in the code....

  2. #47
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,879
    Quote Originally Posted by jkurrle View Post
    One thing I like to do is especially mark out new sections with comment lines. While this may seem obvious, when you start writing code into the thousands of lines, you need sections to really stand out. So, at the beginning and end of major sections, I'll comment something like this:

    PHP Code:
    /*** Some_New_Section ***/
    // some code here...
    /*** End Some_New_Section ***/ 
    The larger or more important the section, the more asterisks I use to delineate it. That way, when I'm quickly paging through the code, I can immediately see where I'm at....
    I'd prefer to just modularize the code more so that none of source files even comes close to a thousand lines.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #48
    To code, or not to code? jkurrle's Avatar
    Join Date
    Jul 2004
    Location
    Gainesville, VA
    Posts
    524
    I agree with that sentiment, but I don't always have the freedom to write as I want. I once tried creating a simple MySQL class for server/database connections and running queries and they almost had a cow! Sometimes you alter legacy code and sometimes it alters you...
    99 little bugs in the code, 99 bugs in the code,
    fix one bug, compile it again...
    101 little bugs in the code....

  4. #49
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,531
    Or you can use a language-sensitive editor that can fold code.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #50
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Quote Originally Posted by laserlight View Post
    Unless there are objections or suggestions otherwise, I am making this a sticky.

    Note that style guidelines are always subjective. Feel free to use a different style, as long as you are consistent and keep to the spirit of what iceomnia suggested.
    I'd like to note that, on my system at least, when I'm posting code manually here and hit Tab it skips on to the submit button, or the next field in the form, so if you're posting a sizable chunk of code manually, perhaps do it in Notepad or another simple text editor beforehand and then paste it in.

  6. #51
    Senior Member iceomnia's Avatar
    Join Date
    Oct 2007
    Posts
    181
    Quote Originally Posted by ellyka112 View Post
    I'd like to note that, on my system at least, when I'm posting code manually here and hit Tab it skips on to the submit button, or the next field in the form, so if you're posting a sizable chunk of code manually, perhaps do it in Notepad or another simple text editor beforehand and then paste it in.
    Well then why not tell your editor to replace tabs with 4 spaces.

    You should be using 4 spaces anyway as this is the industry standard as tabs can be inerpreted differently in different programs.
    *** Please read this when pasting your code into this website ***

    Here is a great article about Correctly Formatting Your Code to make it easily readable and easier to comment for debugging.

  7. #52
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,915
    Quote Originally Posted by iceomnia
    Well then why not tell your editor to replace tabs with 4 spaces.
    That won't solve the problem since this jump of focus by a tab is a feature of the web browser, not the editor.

    Quote Originally Posted by iceomnia
    You should be using 4 spaces anyway as this is the industry standard as tabs can be inerpreted differently in different programs.
    I am not convinced that the use of 4 spaces, or even the use of spaces rather than actual tab characters for indentation, is "industry standard". Various settings related to indentation can be configured in many editors, so the problem is largely a one of consistency within the file, and perhaps within the project or team.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  8. #53
    Senior Member iceomnia's Avatar
    Join Date
    Oct 2007
    Posts
    181
    Quote Originally Posted by laserlight View Post
    Originally Posted by iceomnia
    Well then why not tell your editor to replace tabs with 4 spaces.
    That won't solve the problem since this jump of focus by a tab is a feature of the web browser, not the editor.
    Ha ha - well I'm talking about when code is copied and pasted inot this website.

    If you are just tying into the box - use 4 spaces, or 8 or 16, or whatever - just be consistent.

    PHP Code:
    <?php

    class Indented
    {
        public function 
    indentedFourSpaces($indented)
        {
            if (
    $indented == true) {
                return 
    true;
            }
        }
    }
    ?>
    *** Please read this when pasting your code into this website ***

    Here is a great article about Correctly Formatting Your Code to make it easily readable and easier to comment for debugging.

  9. #54
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Given the state of some of the code that gets posted here, I think I'd be happy with any kind of indentation so long as its more than none.

    For the record, I personally use tab characters to indent. Why? iceomnia said it best:

    Quote Originally Posted by iceomnia
    tabs can be inerpreted differently in different programs.
    Exactly! If I like tabs to be the width of 4 spaces, then I'll tell my editor that my tab width is four. If you like 2, then you can do likewise. The same data will then appear exactly as both of us prefer to see it.

    Why should I be stuck seeing your two spaces when I'd rather see four (and vice versa)? This is all not to mention the fact that a tab character is always 1 character, whereas using spaces means you've got a variable amount (probably more than 1) of characters all over the place for no other reason other than aesthetic whitespace.

  10. #55
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    Something I can't believe I haven't seen mentioned is the way people format variables in strings. There are many options but nothing is more annoying to me than when someone is switching back and forth in a single script. The different formats I've seen are:
    PHP Code:
    // strings
    $string "full";
    echo 
    "The string is $string<br />";
    echo 
    "The string is {$string}<br />";
    echo 
    "The string is ".$string."<br />";
    echo 
    'The string is '.$string.'<br />';
    // will output 4 lines saying The string is full

    //arrays
    $arr = array("full");
    echo 
    "The first array value is $arr[1]<br />";// will not work when using a string as a key
    echo "The first array value is {$arr[1]}<br />";
    echo 
    "The first array value is ".$arr[1]."<br />";
    echo 
    'The first array value is '.$arr[1].'<br />';
    // will output 4 lines saying The first array value is full 
    Personally I have no problem reading any of these styles, however I do get really annoyed when someone refuses to stick to one method. Also note there are a few other syntaxes, but I can't think of them currently.

  11. #56
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,879
    Don't forget comma-separated arguments for echo.
    PHP Code:
    echo 'The string is '$string'<br />'
    Quote Originally Posted by Derokorian View Post
    Something I can't believe I haven't seen mentioned is the way people format variables in strings. There are many options but nothing is more annoying to me than when someone is switching back and forth in a single script. The different formats I've seen are:
    PHP Code:
    // strings
    $string "full";
    echo 
    "The string is $string<br />";
    echo 
    "The string is {$string}<br />";
    echo 
    "The string is ".$string."<br />";
    echo 
    'The string is '.$string.'<br />';
    // will output 4 lines saying The string is full

    //arrays
    $arr = array("full");
    echo 
    "The first array value is $arr[1]<br />";// will not work when using a string as a key
    echo "The first array value is {$arr[1]}<br />";
    echo 
    "The first array value is ".$arr[1]."<br />";
    echo 
    'The first array value is '.$arr[1].'<br />';
    // will output 4 lines saying The first array value is full 
    Personally I have no problem reading any of these styles, however I do get really annoyed when someone refuses to stick to one method. Also note there are a few other syntaxes, but I can't think of them currently.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  12. #57
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    Well I haven't seen that before when working with other's code. Of course I don't work with others code very often.

  13. #58
    Senior Member iceomnia's Avatar
    Join Date
    Oct 2007
    Posts
    181
    You will start to see more obscure formats now you've joined this forum.

    Incidental, the commas in echo commands is said to be more efficient - but that's for another thread.
    Last edited by iceomnia; 04-07-2011 at 04:18 AM.
    *** Please read this when pasting your code into this website ***

    Here is a great article about Correctly Formatting Your Code to make it easily readable and easier to comment for debugging.

  14. #59
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Going from the OPs example, I prefer coding like this:

    PHP Code:
    // Correctly spacing a function with tabs to make it readable. 
    function foo($bar) { 
        
    // Use a coment to tell us what the block below is doing. 
        
    if ($bar == true) { 
            
    $something true
        } 
         
        return 
    $somthing

    If it was a single line, I would further condense this:
    PHP Code:
        if ($bar == true) { $something true; } 
    I hate wasting lines of code with single brackets where they are not needed.
    It may seem like a small change but saves a lot of scrolling when you develop fully fledged systems.

  15. #60
    Senior Member
    Join Date
    Mar 2009
    Posts
    1,153
    Quote Originally Posted by Elev8 View Post
    I hate wasting lines of code with single brackets where they are not needed.
    If a conditional (as well as loops) only has one statement it doesn't need braces at all.

    PHP Code:
    if ($bar == true$something true
    I personally prefer to put it on the next line simply because it's a flow control structure and it makes the code look like it's literally flowing.
    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •