PHP Syntax Formatting - Page 5
Page 5 of 6 FirstFirst ... 3456 LastLast
Results 61 to 75 of 85

Thread: PHP Syntax Formatting

  1. #61
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    Quote Originally Posted by Bonesnap View Post
    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.
    I was gonna say the exact same thing. Of course I still generally use two lines, with indentation:
    PHP Code:
    if( $bar == TRUE 
       
    $something TRUE
    Quote Originally Posted by Elev8 View Post
    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.
    You know what saves even more scrolling? Use a good editor that supports Code Folding
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  2. #62
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Quote Originally Posted by Derokorian View Post
    I was gonna say the exact same thing. Of course I still generally use two lines, with indentation:
    PHP Code:
    if( $bar == TRUE 
       
    $something TRUE

    You know what saves even more scrolling? Use a good editor that supports Code Folding
    Firstly, using braces allows for easy expansion at a later date without the need for trying to find find the end point.
    It does work well in template code, similar to WordPress' layout...
    PHP Code:
    <?php if($x=$y) : ?>
    Random HTML
    <?php else : ?>
    Other HTML
    <?php endif; ?>
    Apart from that case, I find the atypical aspect of using it arbitrarily can cause confusion.


    Secondly, code folding is just terrible IMO.
    Both Dreamweaver and Notepad++ use "folding", but it's just such a bad habit to get into.

    Maybe it's just a bad habit I have developed after being a notepad coder for 15 years.
    Either way, I never have, and never will, use code folding.

  3. #63
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,531
    I use code folding to collapse documentation out of the way, but I don't use it for pairs of braces. Instead I avoid letting the code between them get too long and hairy in the first place. If I find myself checking braces I take it as a sign that "here be long and hairy code" threatens and I work on sorting it out.

    I have braces on their own line so that it takes less conscious effort to pair them visually, raising the threshold where I start deliberately checking. Using a 7-pt font further reduces the amount of eyeball roving involved.
    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. #64
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    How is code folding a bad habit? When looking for a certain function to update I don't even have to scroll, I just expand that function only...

    Also since I tend to comment the way PHPDocumentor says to (which is supported for parsing by my editor) its super nice to get rid of those 40 lines of comments and get to just the code.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  5. #65
    Senior Member
    Join Date
    Mar 2009
    Posts
    1,153
    Quote Originally Posted by Elev8 View Post
    Firstly, using braces allows for easy expansion at a later date without the need for trying to find find the end point.
    Why would you need to "try and find" the end point? The end point is the very next line...

    Quote Originally Posted by Elev8 View Post
    It does work well in template code, similar to WordPress' layout...
    PHP Code:
    <?php if($x=$y) : ?>
    Random HTML
    <?php else : ?>
    Other HTML
    <?php endif; ?>
    I can't stand the way WordPress does this in their template code. I usually end up modifying it to use braces, or remove it entirely and do my own thing.

    Quote Originally Posted by Elev8 View Post
    Secondly, code folding is just terrible IMO.
    Both Dreamweaver and Notepad++ use "folding", but it's just such a bad habit to get into.
    How is it terrible and how is it a bad habit?

    Quote Originally Posted by Weedpacket View Post
    I use code folding to collapse documentation out of the way, but I don't use it for pairs of braces. Instead I avoid letting the code between them get too long and hairy in the first place. If I find myself checking braces I take it as a sign that "here be long and hairy code" threatens and I work on sorting it out.
    I'll use it for functions, especially if I have a PHP file that has a bunch of functions in it. Most editors allow you to hover over the collapsed section and a small "tooltip" of sorts shows you the code that's collapsed, so you can at a glance see the function's code (at least part of it, anyway). I also never find myself "checking" braces since my editor highlights matching braces.

    Quote Originally Posted by Weedpacket View Post
    I have braces on their own line so that it takes less conscious effort to pair them visually, raising the threshold where I start deliberately checking.
    I also put braces on their own line; I feel it makes it not only easier to read, but it also gives literal meaning to "code block".

    Quote Originally Posted by Weedpacket View Post
    Using a 7-pt font further reduces the amount of eyeball roving involved.
    My eyes are bad so I code in size 14pt font.

    Quote Originally Posted by Derokorian View Post
    Also since I tend to comment the way PHPDocumentor says to (which is supported for parsing by my editor) its super nice to get rid of those 40 lines of comments and get to just the code.
    +1 for phpDocumentor comments.
    Declare variables, not war.

  6. #66
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    Using a 7-pt font further reduces the amount of eyeball roving involved.
    My eyes are bad so I code in size 14pt font.
    I have no idea what size font I use, I tend to use the ctrl+mouse scroll a lot to make it bigger or smaller. I tend to make it small to find what I'm looking for then bigger to change it...
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  7. #67
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Quote Originally Posted by Bonesnap View Post
    Why would you need to "try and find" the end point? The end point is the very next line...
    I have never, ever used:
    Code:
    if ($bar == true) $something = true;
    without it expanding to include other code at a later stage.

    I thought that's what the condensed conditional statement was for?

    Code:
    $var = (1 == 2) ? "A" : "B";
    I didn't say it was right or wrong, just my personal preference after many years of coding in notepad.
    I have severe OCD so like my code to be as small and readable as possible.
    I am also an autodidact, so my definition of readable probably isn't the same as yours.

  8. #68
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by Elev8 View Post
    I have never, ever used:
    Code:
    if ($bar == true) $something = true;
    without it expanding to include other code at a later stage.
    I tend to agree.

    However, this is a non-issue for me, since here at work the practice of leaving off {}'s for one-liners like that is expressly prohibited in the coding standards (which is not an uncommon practice).

    Can't same I blame the authors of the standard for doing so, either.

  9. #69
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    I use it a lot for concatenation, especially in my models. This way, I can build queries based on function arguments, so I end up with a few lines of:
    PHP Code:
    if( !is_null($group) ) $sql .= ' AND group = :group';
    // ... after preparing
    if( !is_null($group) ) $stmt->bindParam(':group',   $group,   PDO::PARAM_INT); 
    Of course I don't work for a development company (though I would like to) so this is my personal preference.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  10. #70
    Senior Member
    Join Date
    Mar 2009
    Posts
    1,153
    Quote Originally Posted by Elev8 View Post
    I have never, ever used:
    Code:
    if ($bar == true) $something = true;
    without it expanding to include other code at a later stage.

    I thought that's what the condensed conditional statement was for?

    Code:
    $var = (1 == 2) ? "A" : "B";
    I didn't say it was right or wrong, just my personal preference after many years of coding in notepad.
    I have severe OCD so like my code to be as small and readable as possible.
    I am also an autodidact, so my definition of readable probably isn't the same as yours.
    All I said was that you didn't have to "try and find the end point" since the "end point" would be the very next line. Perhaps I misinterpreted, but I got the feeling from reading what you wrote that it was somehow difficult to determine when the conditional ended.

    Also, I usually write it as...
    Code:
    if ($bar == true)
        $something = true;
    ...simply because it gives literal meaning to "program flow". Also, that way if I do have to add additional lines at a later point it requires minimal effort.

    Lastly, the two coding examples you gave are completely different and the first one would never work as a ternary operator since there's no value being assigned if the condition is false. Sometimes all you want to do is set something to true, or false, or whatever; there's not always an opposite.
    Declare variables, not war.

  11. #71
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by Bonesnap View Post
    the first one would never work as a ternary operator since there's no value being assigned if the condition is false.
    Oh really?

    PHP Code:
    if ($bar == true$something true;

    // same as:

    $bar != true ?: $something true
    (Regardless, I still consider either one to be more obfuscated than it is efficient, thus doing more harm than good.)

  12. #72
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    PHP Code:
    $bar != true ?: $something true
    Gross!
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  13. #73
    Senior Member
    Join Date
    Mar 2009
    Posts
    1,153
    Quote Originally Posted by bradgrafelman View Post
    Oh really?

    PHP Code:
    if ($bar == true$something true;

    // same as:

    $bar != true ?: $something true
    Definitely never seen a ternary operator written like that before. Interesting.

    Quote Originally Posted by bradgrafelman View Post
    (Regardless, I still consider either one to be more obfuscated than it is efficient, thus doing more harm than good.)
    So you never use ternary operators?
    Declare variables, not war.

  14. #74
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by Bonesnap View Post
    So you never use ternary operators?
    Depends on where I'm coding. Here at work (or any other place I've been that has official coding standards), the answer is: no. In the hundreds of thousands of lines of code (perhaps more), you will never find a) the ternary operator, or even b) an if() statement without {} braces.

    If it's just my own personal code (which has been rather non-existent recently), the answer is: occasionally.

  15. #75
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,531
    Quote Originally Posted by Bonesnap
    Definitely never seen a ternary operator written like that before. Interesting.
    I still maintain "ternary operator" is a silly name for it, and the name for it in C ("conditional operator") much better describes its function.

    Especially since $foo ?: $bar is a binary operation.

    Which, not incidentally, is basically equivalent to the || of JavaScript or the or of Python. Unfortunately, PHP as yet has no equivalent of their && or and.
    Last edited by Weedpacket; 02-22-2012 at 07:36 PM.
    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

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
  •