Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

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:

<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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...