开发者

php echo vs open&close tag

Just to clarify: The issues "echo vs print" and "double quotes vs single quotes" are perfectly understood, this is about another thing:

Are there any reasons why one would prefer:

echo '<table>';   
foreach($lotsofrows as $row)
{
    echo '<tr><td>',$row['id'],'</td></tr>';   
}
echo '<table>';

over开发者_如何学Go:

<table><?php
       foreach($lotsofrows as $row)
       { ?>
           <tr>
              <td><?php echo $row['id']; ?></td>
           </tr><?php
       } ?>
</table>

would either one execute/parse faster? is more elegant? (etc.)

I tend to use the second option, but I'm worried I might be overlooking something obvious/essential.


Benefits of first one

  • Easier to read
  • ???

Benefits of second one

  • WYSIWYG is possible
  • HTML Code Completion/Tag-Matching possible with some IDEs
  • No escaping headaches
  • Easier for larger chunks of HTML

If I have a lot of HTML in a given PHP routine (like an MVC view) then I definitely use the 2nd method. But I format it differently - I strictly rely on the tag-like nature of PHP's demarcations, i.e., I make the PHP sections look as much like HTML tags as I can

<table>
  <?php foreach($lotsofrows as $row) { ?>
  <tr>
    <td><?php echo $row['id']; ?></td>
  </tr>
  <?php } ?>
</table>


I agree with Peter Bailey. However, in views I use the alternative syntax for statements, and much prefer short tags (particularly for echoing). So the above example would instead read:

 <table> 
  <? foreach($lotsofrows as $row): ?>
  <tr>
    <td><?= $row['id']; ?></td>
  </tr>
  <? endforeach; ?>
 </table>

I believe this is the preferred standard for Zend Framework.


The first is far more readable in my opinion, however, the second technically involves less parsing. Any speed advantage in that case would likely be minor and really meaningless without profiling.

Premature Optimization is the root of all evil. Do what makes the code easiest to read and maintain. Efficiency takes a backseat to maintainability.

see http://en.wikipedia.org/wiki/Optimization_%28computer_science%29#When_to_optimize for some good advice on the subject


It's very dependable what you write. PHP can be used as programming language, or as simple and powerful web-template language. Mixing of this two usages very, very bad practice and will be horrible to support in long term.

So Second style is more usable in templates with lot of html markup and little spots of code, first - for 'clear' php programming.


The best one is a template engine.

But, I think echo is way more cleaner and more readable (at least in this case - as pointed out in comments, it depends), than opening and closing tags everywhere (I don't know too much about PHP internals to tell which one is faster, though).


First one is more readable from programming point of view, but the second one allows you to open the file in some WYSIWYG HTML editor and change the page design.

I prefer the second option because it is much easier to tell your designer that "this part of the page will behave like that", than "this piece of code does that"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜