PHP Syntax Formatting - Page 3
Page 3 of 6 FirstFirst 12345 ... LastLast
Results 31 to 45 of 85

Thread: PHP Syntax Formatting

  1. #31
    Deviloper ixalmida's Avatar
    Join Date
    Jul 2008
    Location
    Chicago-area
    Posts
    554

    Formatting your code

    PHP Parse Error: syntax error, unexpected T_STRING in showpost.php on line 7
    -- Haha!

    I was poking around today and found out that there are actually names for the styles we are talking about! My style is called "Whitesmiths" as opposed to the "GNU/Pear" style that others here have mentioned.

    If formatting your code is just too much trouble for you, try this site: PHP Formatter

    You can paste your code into the window, select your preferred style, and click a button and it will make your code all pretty for you. Pretty cool...

  2. #32
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,840
    I just type Ctrl-F in my editor and it does it for me.
    "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. #33
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,503
    Quote Originally Posted by ixalmida
    You can paste your code into the window, select your preferred style, and click a button and it will make your code all pretty for you. Pretty cool...
    If you don't keep your code formatted while you're writing it - pretty much on every line you write as you write it - you're missing a big chunk of the reason why you should be formatting it.

    Using that site every time you type a line seems like a little too much trouble
    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

  4. #34
    Deviloper ixalmida's Avatar
    Join Date
    Jul 2008
    Location
    Chicago-area
    Posts
    554
    Using that site every time you type a line seems like a little too much trouble
    Agreed. However, if you're having to fix someone else's code the site can be really helpful.

    I inherited a mission-critical site. The previous developer wasn't really a developer. Not only did he not format his code, but he used modular pages (not to be confused with OOP), so it is impossible to change something without potentially affecting a bunch of other pages.

    For example, the main index page loads 6 different modules (layed out with Javascript), and those modules reference or load other modules based on a user's access level. When you click a link, it loads a new module not a new page.

    It gets complicated very quickly and there is NO documentation. It is also very slow in places, doesn't take advantage of CSS ,and is difficult to optimize. So I'm pretty much looking at rebuilding from scratch a financial site that took 3 years to build.

    (Sorry folks, just had to vent a little!)

    I just type Ctrl-F in my editor and it does it for me.
    Okay...could you enlighten us on what editor you're using? Ctrl-F is mapped to the "find" function in every editor I'm aware of.
    Last edited by ixalmida; 01-09-2009 at 11:40 AM.

  5. #35
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,840
    Quote Originally Posted by ixalmida View Post
    ...
    Okay...could you enlighten us on what editor you're using? Ctrl-F is mapped to the "find" function in every editor I'm aware of.
    Oops, should've been "Ctrl-Shift-F".

    phpDesigner is the editor/IDE. I don't remember if that was the default key-mapping for the format command or if I overrode it at some point. Aptana has a similar function, so I wouldn't be surprised if some of the other Eclipse-based IDE's also do.
    "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

  6. #36
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,503
    Quote Originally Posted by NogDog
    Aptana has a similar function, so I wouldn't be surprised if some of the other Eclipse-based IDE's also do.
    You'd be right; the same keybinding is the default in Zend Eclipse and Eclipse itself.
    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

  7. #37
    Senior Member iceomnia's Avatar
    Join Date
    Oct 2007
    Posts
    181

    PHP Syntax Formatting

    In relation to my previous post back in 2008 regarding correctly spacing your code to make it eaasily readable, I have found many articles supporting my recommendation for using in the Allman Style syntax formatting.

    This is where the main rules are that each new curly brace goes on a new line.

    I have also found this on expressionengine.com about general syntax formatting.

    I particularly like this syntax style as it makes it incredibly easy to comment out braces to enable certain flow.

    Allman style (bsd in Emacs)

    Any views, comments, agreements, disagreements?
    *** 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.

  8. #38
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,913
    Quote Originally Posted by iceomnia
    I have found many articles supporting my recommendation for using in the Allman Style syntax formatting.
    If you have not found many articles supporting other indent styles, then either you have not searched hard enough, or the supporters are too busy coding to evangelise

    Quote Originally Posted by iceomnia
    I have also found this on expressionengine.com about general syntax formatting.
    So what? That is just a style guideline that I am not required to follow

    Quote Originally Posted by iceomnia
    I particularly like this syntax style as it makes it incredibly easy to comment out braces to enable certain flow.
    That sounds dubious: why would you be commenting out braces?

    I like it for a different reason, related to one cited by the "Technical Spartacus" blog that you linked to: I would rather not extend code horizontally, so I try to break to a new line after about the 80th column. This new line of code would then be indented by one level. Without the opening brace being on its own line, there would then be no visual cue as to the difference between the continuation of the previous line of a control statement and the first line of the block. (But note that some style guidelines suggest a two level indentation for continuation lines instead, in which case this "problem" would no longer apply. But I dislike the extra level of indentation.)

    Of course, I disagree with Spartacus' implication that the Allman style is the "correct" indent style.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  9. #39
    Senior Member
    Join Date
    Apr 2009
    Location
    Hamburg, Germany
    Posts
    588
    http://en.wikipedia.org/wiki/Indent_style

    I found this following your links iceomnia - there is some variety out there as laserlight already pointed out.

    Btw: we are coding PHP here, so imho using Zend Coding Standard seems like a relatively intuitive choice.

    Bjom

  10. #40
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,503
    Yes, that standard was referred to in the thread iceomnia referred to.

    Other threads on the subject can be found in the Echo Lounge (which it surprised me not to find in the thread)
    http://www.phpbuilder.com/board/show...php?t=10256498 (which is actually in Code Critique, now)
    http://www.phpbuilder.com/board/show...php?t=10298088
    http://www.phpbuilder.com/board/show...php?t=10344137
    http://www.phpbuilder.com/board/show...php?t=10234388

    And this thread might as well be merged with the original under the current name.
    Last edited by Weedpacket; 05-03-2009 at 12:20 AM.
    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

  11. #41
    Senior Member iceomnia's Avatar
    Join Date
    Oct 2007
    Posts
    181
    Quote Originally Posted by laserlight View Post
    If you have not found many articles supporting other indent styles, then either you have not searched hard enough, or the supporters are too busy coding to evangelise
    No I have it's just I can find so many more supporting reasons to adopt the Allman Style formatting.

    Quote Originally Posted by laserlight View Post
    So what? That is just a style guideline that I am not required to follow
    Ha ha - I don't think that everyone should follow one style. But It is quite irritating when you are used to one style and a fellow (colleague) programmer codes in a completely different style.

    Quote Originally Posted by laserlight View Post
    I like it for a different reason, related to one cited by the "Technical Spartacus" blog that you linked to: I would rather not extend code horizontally, so I try to break to a new line after about the 80th column. This new line of code would then be indented by one level. Without the opening brace being on its own line, there would then be no visual cue as to the difference between the continuation of the previous line of a control statement and the first line of the block. (But note that some style guidelines suggest a two level indentation for continuation lines instead, in which case this "problem" would no longer apply. But I dislike the extra level of indentation.)

    Of course, I disagree with Spartacus' implication that the Allman style is the "correct" indent style.
    I don't agree either that the Allman Style is "CORRECT". However, I have adopted it as I find it makes the code feel the neatest and it seems to flow better.

    Also I use Zend Studio for Eclipse and I have set up my ccode formatter to use the Allman Style by default.

    Quote Originally Posted by Bjom View Post
    http://en.wikipedia.org/wiki/Indent_style
    I found this following your links iceomnia - there is some variety out there as laserlight already pointed out.

    Btw: we are coding PHP here, so imho using Zend Coding Standard seems like a relatively intuitive choice.
    Bjom
    I agree that the Zend syntax formatting method is pretty adoptable, but they don't tend to put " if statement braces " on a new line which would make it difficult to match up the corresponding braces.
    *** 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.

  12. #42
    Senior Member iceomnia's Avatar
    Join Date
    Oct 2007
    Posts
    181
    Quote Originally Posted by ixalmida View Post
    -- Haha!

    I was poking around today and found out that there are actually names for the styles we are talking about! My style is called "Whitesmiths" as opposed to the "GNU/Pear" style that others here have mentioned.

    If formatting your code is just too much trouble for you, try this site: PHP Formatter

    You can paste your code into the window, select your preferred style, and click a button and it will make your code all pretty for you. Pretty cool...
    I only just noticed that poost! - That is an amazing tool!

    I just tried it with some really sloppy code and it formatted it perfectly (sorry - formatted it favourably).

    Although - it does look like it may be indenting by two spaces and my adpoted standard is 4.
    *** 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.

  13. #43
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,913
    Quote Originally Posted by iceomnia
    No I have it's just I can find so many more supporting reasons to adopt the Allman Style formatting.
    I am cautious about such evaluations, because for these kind of personal preference issues, we especially tend to trump up what suits our preference and downplay what does not. Indeed, what we consider important may be considered inconsequential by others.

    Quote Originally Posted by iceomnia
    But It is quite irritating when you are used to one style and a fellow (colleague) programmer codes in a completely different style.
    Sure, but minor issues of style are like that, so you should just get used to the variance. I have written code for a project whose core developers prefer tabs to spaces. Change my editor setting and move on. I try my best to match the style of users of this forum when replying to their posts, assuming that they actually use a consistent and reasonable style.

    Yes, I have a hard time writing code to meet Whitesmiths style indenting, but that is because I have never been involved in a project where that was the preference, so I lack practice. Reading? No problem, as long as it is well indented.

    Quote Originally Posted by iceomnia
    I agree that the Zend syntax formatting method is pretty adoptable, but they don't tend to put " if statement braces " on a new line which would make it difficult to match up the corresponding braces.
    There is no need "to match up the corresponding braces". For one thing, the indentation itself is enough. If not, then a good editor with code folding can be used. This is an example of "what we consider important may be considered inconsequential by others"
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  14. #44
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,503
    Quote Originally Posted by iceomnia
    I agree that the Zend syntax formatting method is pretty adoptable, but they don't tend to put " if statement braces " on a new line which would make it difficult to match up the corresponding braces.
    Works for me. Mostly: it refuses to put "else" and "elseif" on a new line - they always end up at the end of the previous line even if the previous line isn't a "}". Either that or I just haven't found that setting (despite fiddling with every setting I could find). It's like the "Braces" settings only apply to opening braces.

    Quote Originally Posted by laserlight
    There is no need "to match up the corresponding braces". For one thing, the indentation itself is enough. If not, then a good editor with code folding can be used.
    And that shouldn't be necessary too often, either. Below the function/class level, I think about the only time I have more than twenty lines between braces is when I'm doing a switch statement; and then there are the labels to break things up.
    Last edited by Weedpacket; 05-03-2009 at 07:22 AM.
    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

  15. #45
    Senior Member cbj4074's Avatar
    Join Date
    Feb 2004
    Location
    Portland, ME
    Posts
    225
    A Zend Studio user here, too. Hot-keyed code formatting that can be customized serves nearly all required purposes, in my personal case. As others have indicated, the Zend formatter is not perfect, but it works well enough for most.

    The big one for me is being able to re-format entire "projects" at once. It makes dealing with other peoples' shoddy code less cumbersome in that one can "convert" an entire project to his or her own style of preference and return the "finished" code in the "project's" preferred format. Simple, elegant.

    A couple of screen caps of the interface are attached for those who are interested.
    Attached Images Attached Images
    Last edited by cbj4074; 11-19-2009 at 10:15 PM. Reason: Someone already said it. Oops.
    To be wise is to listen exactly as often as you speak.

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
  •