SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which is better to do, bulk echo of form or dip in and out of php where required

    Hi, I was just wondering if it was quicker to dip in and out of PHP in your markup or output a bulk of html in one echo?

    Example,

    Would...

    PHP Code:
    <form action="blah" method="post">
     <input type="submit" name="<?php echo $fieldnameSubmit?>" value="<?php echo $submitButtonText?>" />
    </form>
    Be quicker for the parser than...
    PHP Code:
    <?php
    echo '
    <form action="blah" method="post">
     <input type="submit" name="'
    .$fieldnameSubmit.'" value="'.$submitButtonText.'" />
    </form>'
    ;
    ?>
    This is obviously a small form, but imagine it has lots of fields for a registration form for example. On a larger form the parser would be dipping in and out of and between html and php whereas for the second one it has more to output but hasn't got to start, stop sort of speak.

    Thanks

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The difference is so small that it's irrelevant - but you can run your own benchmarks if you really want to know . Think about which one looks cleaner and is easier to maintain. If you output html in php template files then most people would use the first version because such files are more html than php and you get all the benefits of html syntax highlighting in your editor if you drop out of the php mode - whereas html in echo is just a string of plain text. The best solution, I think, is to use the short php tags:
    PHP Code:
    <form action="blah" method="post">
     <input type="submit" name="<?=$fieldnameSubmit ?>" value="<?=$submitButtonText ?>" />
    </form>

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    The best solution, I think, is to use the short php tags:
    PHP Code:
    <form action="blah" method="post">
     <input type="submit" name="<?=$fieldnameSubmit ?>" value="<?=$submitButtonText ?>" />
    </form>
    Be careful with short tags, they are not always enabled, and keep in mind they can be disabled. Not that there is anything fundamentally wrong with them, just keep that in mind.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cpradio View Post
    Be careful with short tags, they are not always enabled, and keep in mind they can be disabled. Not that there is anything fundamentally wrong with them, just keep that in mind.
    Most hosts aren't there yet, but as of PHP 5.4 they can no longer be disabled; thus are always enabled. Just for future reference

    Edit:


    That only goes for <?= , not for e.g. <? echo $something;

    See http://php.net/manual/en/ini.core.ph...short-open-tag


    As for the original question I agree that the difference is negligible and you should go with whichever you like better.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Member
    Join Date
    Sep 2012
    Location
    Dhaka, Bangladesh
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Most hosts aren't there yet, but as of PHP 5.4 they can no longer be disabled; thus are always enabled. Just for future reference
    Thanks for the information. I didn't know.

  6. #6
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I must admit, I tend to echo all my html, but I must add that using short tags does make the code easier to read and saves a lot fiddling around if you want to copy the html code into an html file - I thinks it's down to personal preference really. I've never come across a server where short tags are disabled though.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for your input . Think i'll use both depending on the situation then, thanks.

  8. #8
    SitePoint Member
    Join Date
    Sep 2008
    Location
    Alpharetta, GA USA
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    Think about which one looks cleaner and is easier to maintain. If you output html in php template files then most people would use the first version because such files are more html than php and you get all the benefits of html syntax highlighting in your editor if you drop out of the php mode - whereas html in echo is just a string of plain text.
    Good advice, IMO. I've definitely come to prefer that first method, which some call "Medium PHP" style.

    Jeff


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •