Question: How do you format your code?

Discussion in 'PHP' started by PoPSiCLe, Mar 16, 2009.

  1. #1
    Reason I'm asking is that I'm currently "cleaning up" lots of old code-files which are still used on a page I run - by cleaning up I mean improving the code, removing unused and redundant coding etc.

    And, while doing this, I try to also clean up the general code - I mean the HTML embedded etc.

    And here comes the main part of my query, so to speak:

    Before, I've had lots of "echo"-statements, and such, and I find that these obfuscate the code somewhat - I've also grown tired of a "mix and (mis)match" of singel and double quotes, and I'm trying to fix all such stuff (yes, I am very nitpicky when it comes to cleaning up, when I have the time to do so).

    Therefore, I've now moved to escaping and restating php (almost) every time I use HTML - ie. <?php and ?> are used lots in my files, at least "lots" in the short functions and such.

    So, how do you people feel about this? Is it annoying that the <?php and ?> "pops up" every other line, or is this a good way of keeping the PHP-stuff and the HTML-stuff separated while still having both in the same file?
     
    PoPSiCLe, Mar 16, 2009 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    It's arbitrary more or less. I see many programmers who prefer

    <?php
    /**
     * Lines of code...
     * blah
     * blah...
     *
     */
    ?>
    <!-- HTML Here -->
    <?php
    /**
     * Continue...
     *
     */
    ?>
    <!-- Continue HTML -->
    
    PHP:
    Personally, however, I use the print function. Coming from C++ into PHP, I like to define my own classes and functions for most of my web apps too so I use a lot of returns, etc.

    For example:
    <?php
    function test($id=NULL){
      if(!$id){
        return false;
      }
      return true;
    }
    
    $testvar = test('works');
    //$testvar = test(); // This would print the fail message! =-O
    if(!$testvar){
      print "You did not enter anything for test()!";
      exit;
    } else {
      print "Success!";
    }
    ?>
    PHP:
    So everything for me is used in conditionals anyway. But a lot of the time I use variables then print out a template file (obviously it would run through the template and replace with the proper vars before printing)

    :)

    Regards,
    Dennis M.
     
    Dennis M., Mar 16, 2009 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think it's really a matter of personal taste.

    I tend to do about the opposite - Everything is done with echo (if output buffering, otherwise string concatenation), and I freely make use of single and double quotes, whichever is appropriate. All the sites I do are fully database-driven and I use utility classes to do most of the heavy lifting, so it's rare that I have more than two or three lines of HTML anyway. The only longer stretch is in my page-assembly method, which at its core looks something like this (using the heredoc syntax):

    $head = join("\n\t", $this->head_parts);
    $content = '';
    foreach ($this->content_parts as $id => $html)
       $content .= "\n\t<div id='{$id}'>{$html}</div>\n";
    
    echo <<<EOF
    {$this->doctype}
    <html>
    <head>
       <title>{$this->title}</title>
       {$head}
    </head>
    <body>{$content}</body>
    </html>
    EOF;
    Code (markup):
    I find the <? stuff really distracting, especially when there are a lot of variables being substituted, or methods invoked to prepare code - which is almost always the case. Also it makes it hard for me to format the PHP in a way that's easy to skim through later on. My office is a mess, but I am a neat-freak when it comes to code.
     
    SmallPotatoes, Mar 16, 2009 IP
  4. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #4
    This is how I do it :
    <?php
    // Include files, start session, etc. //
    ?>
    
    <!-- HTML head -->
    
    <?php
    // Page content goes here
    ?>
    
    <!-- HTML end -->
    PHP:
     
    ActiveFrost, Mar 16, 2009 IP
  5. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #5
    Other posters have stated that the repeated in and outs causes more load on the server. I don't have a clue whether that is true or not.

    I do whatever works. I really prefer the clean look you are going for, it just isn't always practical.
     
    Colbyt, Mar 16, 2009 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    I don't really understand why it should "stress" the server more to have it end and restart the parsing of PHP, unless the starting of the parsing is more stressing on the server than actual parsing - I dunno about this though, so I can't say for sure.

    And... the file is still being parsed by the php-engine, I assume, as long as the file-type is set to .php - ie. it won't change the parsing engine to "stop and start" php over and over in the same file?

    I think I'm gonna stick to having my html-code separate (as much as possible, at least) from my php-code.
     
    PoPSiCLe, Mar 16, 2009 IP
  7. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    From what I've read in the past, going out of then back into php is more efficient than echoing except when it's a very short string - eg: 7-8 characters.
     
    Pos1tron, Mar 16, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I suspect that in the long run you'll make the same discovery I did. When I was starting out, the bulk of the code in my files was HTML, and PHP was a minority. It made sense to escape back and forth with <? and ?>.

    Years later, when I was producing files that were almost all PHP (95% or more) and very little HTML, it became more economical for me to use echo.

    As I look over my old code, I can see a smooth and steady progression from all <? to none.

    P.S. I don't know whether escaping back and forth between HTML and PHP is more efficient than using echo statements. But I have done some tests which seem to show that:

    1) "echo" is faster than "print".

    2) "echo $a, $b, $c;" is faster than "echo $a . $b . $c;" (if they are long strings).

    3) any sort of outputting is faster with output buffering turned on.

    4) the single most effective thing you can do to improve the speed of virtually all of your pages, from an effort-to-reward ratio standpoint, is to use gzip compression (most trivially implemented using "ob_start('ob_gzhandler');" at the top of your script). Load times can easily go down by 75%.
     
    SmallPotatoes, Mar 16, 2009 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    That might be - I have a feeling that the PHP-code takes up about 60-70% of each file as of now, maybe a bit more - I haven't really checked :)

    As for the gzip, it's already turned on, and running on all pages, so that's done.
     
    PoPSiCLe, Mar 16, 2009 IP
  10. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I rather have no html code at all in my logic that's the best way to go. So I just use a templating system of some kind which in most cases is PHP itself. So it would be something like this:

    
    $this->render('template');
    
    PHP:
    Template:

    
    <?php foreach ($this->var as $var): ?>
    I am <?= $var['aaa']; ?> <br/>
    <?php endforeach; ?>
    
    PHP:
    Speed does not mean a whole lot if you cannot effectively manage the code. The gains of 0.005ms is not worth the cost in development to maintain a mess.
     
    InFloW, Mar 17, 2009 IP