1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Which is more efficient? ECHO VS HTML

Discussion in 'PHP' started by MCJim, Jun 8, 2008.

  1. #1
    Is it more efficient to echo out a segment of html code inside of your php code, or is it more efficient to end the php code segment and switch to html?

    Example:

    <?php
    //there would be a bunch of php code before this line
    echo '<form>
    First name:
    <input type="text" name="firstname">
    <br>
    Last name:
    <input type="text" name="lastname">
    </form>';
    // there would be a bunch of php code after this line
    ?>

    VERSUS

    <?php
    //there would be a bunch of php code before this line
    ?>
    <form>
    First name:
    <input type="text" name="firstname">
    <br>
    Last name:
    <input type="text" name="lastname">
    </form>
    <?php
    //there would a bunch of php code after this line
    ?>
     
    MCJim, Jun 8, 2008 IP
  2. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #2
    I prefer the second approach as I think I save some *not required* PHP processing this way.
     
    Lordo, Jun 8, 2008 IP
  3. ckgni

    ckgni Active Member

    Messages:
    208
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Yes the second approach puts lower load on the server because the HTML code is out of PHP blocks and does not need server side processing.

    But we are talking about very little difference especially on the high end servers used today. So choose what is more comfortable for you. :)
     
    ckgni, Jun 8, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    I use plain html

    No need to echo

    Regards

    Alex
     
    kmap, Jun 8, 2008 IP
  5. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #5
    There is another merit for the second approach: my clients find it very easy to modify the design without having to worry about the code (more like template approach).
     
    Lordo, Jun 8, 2008 IP
  6. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Does using '<?php' lose any efficiency though? If I use the second approach all the time, I often have to embed <?php and ?> within my form.
     
    MCJim, Jun 8, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Plain HTML is much faster for blocks of text (in comparison), it means PHP doesn't have to parse it.

    It's faster.

    If however you're parsing variables within the HTML, it may be easier just to keep within PHP.
     
    Danltn, Jun 8, 2008 IP
  8. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It may be easier to do otherwise, but is parsing variables within HTML more efficient than keeping the entire code in PHP?
     
    MCJim, Jun 8, 2008 IP
  9. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #9
    As long as it's in a single call, and not multiple calls, and you ensure it's echo, and using commas to concatenate. It's faster, but remember. The difference is NANOseconds.

    Dan
     
    Danltn, Jun 8, 2008 IP
  10. fireflyproject

    fireflyproject Active Member

    Messages:
    969
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    70
    #10


    I can't remember the last time I used <?php I always, without exception use <? especially when just echoing one variable.

    For instance, let's say you wanted to put a value into a text field. Here is how I would do it.

    <input type="text" name="name" value="<?=$variable?>" />
    PHP:
    See? Quick and easy!
     
    fireflyproject, Jun 8, 2008 IP
  11. joffer

    joffer Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The fastest way is the second way since the html code is not processed through the php processor
     
    joffer, Jun 11, 2008 IP
  12. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #12
    ofcource the HTML coz the server doen't have to decode the scripting! :)

    Nuff said! :)
     
    EricBruggema, Jun 11, 2008 IP
  13. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If you are using single quotes, php won't parse any of the text you are outputting, whereas with double quotes it will, which is slower.

    Eg 'blah $variable' will output blah $variable but
    "blah $variable" will output blah <whatever $variable is>
     
    matthewrobertbell, Jun 11, 2008 IP
  14. cashprior

    cashprior Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I wouldn't be much concerned for a small piece of code, I would say it's up to you, use what ever looks cleaner/easier to you.

    I use both ways, but prefer to use php for what it was made for.
     
    cashprior, Jun 12, 2008 IP
  15. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #15
    To the person a few posts above, <?php is generally preferably to <? because <? requires the short tags setting to be on, or won't be parsed.

    Dan
     
    Danltn, Jun 12, 2008 IP
  16. cashprior

    cashprior Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Dan, you are right, it's best to use the full tag <?php.

    It doesn't hurt to know your php configs and how to change them though... (if you have access to) :)
     
    cashprior, Jun 12, 2008 IP
  17. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #17
    Well, I like the second way. That way the text editor can still make out the syntax, plus you don't have to escape everything lol.
     
    clinton, Jun 12, 2008 IP
  18. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #18
    I think the first one is faster then the second one....lol
    This is my own idea but I would like to take a look....I think when everytime I open a php open tag (<?php) its like when I open a new connection to the database, so its gonna take a little bit amount of time to process that.

    Don't mock me if I'm wrong, I just only guess..lol
     
    php-lover, Jun 12, 2008 IP
  19. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #19
    I think .php files are fully php executable anyways, so even if you don't open a tag it's still opening a php application because of the application/php5 that the server executes on. I think both methods are around the same speed though.
     
    clinton, Jun 12, 2008 IP
  20. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #20
    Ooops, no it doesn't. You need to open a php tag in order for the php engine combiler to recognize a php code.
     
    php-lover, Jun 12, 2008 IP