Which of these 2 is better php practice?

Discussion in 'PHP' started by JEET, Apr 16, 2010.

  1. #1
    Hi,
    Which of these is better to do?

    My concerns are: security, speed of execution, memory, code readability, and most important user friendliness.

    <?php echo '<html><body>'. $content. '<br><br>'. $footer. '</body></html>'; ?>

    or
    <html><body><?php echo $content ?><br><br><?php echo $footer ?></body></html>

    The first method is taking 3.1000 microseconds
    The second method is taking 3.6000 microseconds
    Thanks :)
     
    Last edited: Apr 16, 2010
    JEET, Apr 16, 2010 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    There's probably not a lot in it and likely down to personal preference. I'd use the second one since it separates html as html and php as php and hence would probably be easier to debug/read in your editor.
     
    mfscripts, Apr 16, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    The latter is better practice; there is no need to concatenate the strings where you are outputting completely different areas, such as HTML, the content and your footer.

    If you later on need to manipulate the data in some way you will, usually, have to seperate the concatentation anyway, and it makes it easier to debug if you have any problems.
     
    lukeg32, Apr 16, 2010 IP
  4. Andrew E.

    Andrew E. Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The difference is not that huge.

    Try to keep basic HTML out of PHP code, it can cut down on what the php engine has to parse. It also keeps management of code easier.

    If you have to print a string with variables in it, take advantage of the sprintf and printf functions.
     
    Andrew E., Apr 16, 2010 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #5
    I find myself going with the two options below which are variations on a theme. The difference is neglible when you consider that this site is fast and it churns through a huge database load and lots of logic and output. My priorities would be towards readability and maintenance. I would, however, stress that only simple pages should work like this. All others should have some form of MVC or templating being used. Logic and presentation don't belong together.
    I don't see security being an issue when it comes to output, that comes earlier.
    <?php 
    echo '<html><body>', $content, '<br><br>', $footer, '</body></html>'; 
    //or
    echo "<html><body>{$content}<br><br>{$footer}</body></html>";
    ?>
    PHP:
     
    sarahk, Apr 16, 2010 IP
  6. markup

    markup Peon

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There is no big difference , if you are doing lot of php work you can use what you like
     
    markup, Apr 17, 2010 IP
  7. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <html><body><?php echo $content ?><br><br><?php echo $footer ?></body></html>

    It's my choose :D
     
    trickyshaun, Apr 17, 2010 IP
  8. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The second method is more readable but uses 2 PHP tags meaning your server would have to enter and leave the PHP engine twice. I would have to say the best practice is to use a Templating Engine like smarty or make a custom one :)
     
    Brandon.Add.On, Apr 17, 2010 IP
  9. s.ham

    s.ham Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    2nd one is better so that you can beautify your script using some editor as well.
     
    s.ham, Apr 21, 2010 IP
  10. devidhood

    devidhood Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    hiiii
    i think 2nd line is better in developer point of view becoz its differ both html and php

    i hope this will help u.

    thanks

    devid
     
    devidhood, Apr 21, 2010 IP
  11. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #11
    I would go with:

    <html><body><?php echo $content ?><br><br><?php echo $footer ?></body></html>
    PHP:
    But modified slightly to:

    <html><body><?=$content?><br><br><?=$footer?></body></html>
    PHP:
    If I new for certain all servers running the php would have short tags running to avoid compatibility issues.
     
    Silver89, Apr 21, 2010 IP
  12. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #12
    Quick note: to use short php tags make sure short_open_tag is set to 1 within your php.ini.
     
    mfscripts, Apr 22, 2010 IP