14

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.

AlexMelw
  • 2,406
  • 26
  • 35
Migs
  • 825
  • 1
  • 9
  • 16
  • Thanks for all the comments, I'm aware that this is not a question that has THE answer, but it begs some attention are there is nothing official in the PHP manuals and the feedback provided has been extremely useful: I was unaware the first options is far more readable than the second, so I will continue using tags for large blocks and start using "echo" for short ones. – Migs Sep 08 '09 at 15:43
  • I agree with the per-project sentiments. If you have a lot of PHP and the occasional HTML tag, use the former. But if you have a lot of HTML and the occasional PHP command, use the latter (use HTML and stick in PHP tags here and there); it *should* be faster since the tags are already there as literals whereas printing them means passing the tags as strings to the print/echo function, which then prints them out. This can add up for a primarily HTML page, but won’t be much for a primarily PHP page. – Synetech Jun 12 '11 at 16:52

6 Answers6

11

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>
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • 1
    I like this even better than my own answer, so +1 from me :) – Milan Babuškov Sep 08 '09 at 15:18
  • `I strictly rely on the tag-like nature of PHP's demarcations` I think this part is the most important part of advise instead of thinking about using echo / heredoc / phpTag as everyone will reach this conclusion for sure after a long time coding – Saghachi Apr 30 '23 at 03:46
9

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.

voidstate
  • 7,937
  • 4
  • 40
  • 52
  • You got there before me ;) This is the syntax I use most of the time(though I'd put that foreach on its own line). – DisgruntledGoat Sep 08 '09 at 15:33
  • Hi there voidstate, I have usually avoided short tags because of the documentation phrase: "Using short tags should be avoided when developing applications or libraries that are meant for redistribution" but then again, the community has spoken with most of the votes, what have I missed? – Migs Sep 08 '09 at 21:41
  • Where I work I have control over the servers so it's not an issue for me and it makes views much more readable IMO. Here's the Zend Framework doc: http://framework.zend.com/manual/en/zend.view.html#zend.view.introduction.shortTags It suggests changing the php_ini setting in a .htaccess file using the following: php_value "short_open_tag" "on" – voidstate Sep 15 '09 at 10:49
  • Short tags are being removed in PHP 6. – defines Oct 30 '09 at 13:15
6

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

Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
3

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.

Alexey Sviridov
  • 3,360
  • 28
  • 33
2

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

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • 1
    Readability greatly depends on how much text is emitted through `echo`, imho. For a single line closing and opening PHP tags is overkill ... for a complete table row with, like, 20 columns, it's a different story. Also text editors can highlight HTML outside of PHP tags but usually not within PHP strings. Again, that's mostly an issue if there is *much* markup that should be emitted. – Joey Sep 08 '09 at 15:10
  • I still think template engine is best for that. :) But you're right. – Cat Plus Plus Sep 08 '09 at 15:12
  • 2
    IMHO, if you use some kind of MVC engine (and you really should), template system is completely useless. From what I've seen in practice, template systems are either used by web designers who don't know enough PHP to understand what the code does or by programmers who are yet to discover the MVC pattern. – Milan Babuškov Sep 08 '09 at 15:17
  • 1
    Actually, the way of using HTML with some PHP in between is a pretty capable template engine in itself already. Not the prettiest one, but not the crappiest one either :-) – Joey Sep 08 '09 at 15:20
  • I disagree. Template inheritance, variable auto-escaping - those are useful things in my opinion, and it's nice to have a dedicated library that hides their implementation details, and lets you focus on the template itself. And I don't see how templates couldn't be views in MVC. – Cat Plus Plus Sep 08 '09 at 15:40
  • Zend Framework manages both template inheritance and variable auto-escaping using PHP as a templating language with its MVC architecture. I agree that templating languages are an unecessary level of complication if you're able tp understand enough PHP to use that to output variables to the template. – voidstate Nov 02 '09 at 09:43
2

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"

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
  • +1, the second solution makes life easier with designers that do not know PHP and don't want to learn. Still, the optimal solution would be templates, but... – Adriano Varoli Piazza Sep 08 '09 at 15:32