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?



<form action="blah" method="post">
 <input type="submit" name="<?php echo $fieldnameSubmit; ?>" value="<?php echo $submitButtonText; ?>" />

Be quicker for the parser than…

echo '
<form action="blah" method="post">
 <input type="submit" name="'.$fieldnameSubmit.'" value="'.$submitButtonText.'" />

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.


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:

<form action="blah" method="post">
 <input type="submit" name="<?=$fieldnameSubmit ?>" value="<?=$submitButtonText ?>" />

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 :slight_smile:


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

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

As for the original question I agree that the difference is negligible and you should go with whichever you like better.

Thanks for the information. I didn’t know. :slight_smile:

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.

Ok thanks for your input :). Think i’ll use both depending on the situation then, thanks.

Good advice, IMO. I’ve definitely come to prefer that first method, which some call “Medium PHP” style.