<?php ?> Versus Echo Command [duplicate] - php

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.

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"

Related

Opening and closing PHP tags several times in one document [duplicate]

This may be a silly question, but as someone relatively new to PHP, I'm wondering if there are any performance-related issues to frequently opening and closing PHP tags in HTML template code, and if so, what might be best practices in terms of working with PHP tags?
My question is not about the importance/correctness of closing tags, or about which type of code is more readable than another, but rather about how the document gets parsed/executed and what impact it might have on performance.
To illustrate, consider the following two extremes:
Mixing PHP and HTML tags:
<?php echo
'<tr>
<td>'.$variable1.'</td>
<td>'.$variable2.'</td>
<td>'.$variable3.'</td>
<td>'.$variable4.'</td>
<td>'.$variable5.'</td>
</tr>'
?>
// PHP tag opened once
Separating PHP and HTML tags:
<tr>
<td><?php echo $variable1 ?></td>
<td><?php echo $variable2 ?></td>
<td><?php echo $variable3 ?></td>
<td><?php echo $variable4 ?></td>
<td><?php echo $variable5 ?></td>
</tr>
// PHP tag opened five times
Would be interested in hearing some views on this, even if it's just to hear that it makes no difference.
Thanks.
3 simple rules for you to get it right:
No syntax issue can affect performance. Data manipulation does.
Speak of performance only backed with results of profiling.
Premature optimization is the root of all evil
Performance issues are quite hard to understand. It is advised for the newbies not to take it into account. Because they are always impressed with trifle things and fail to see a real important things. Just because lack of experience.
Same for your question. Imagine you'll ever get some difference. Even big one, say, one method is 2 times faster. Oh my, 2 times! I choose it and optimized my app well, it will run 50% faster now!
Wrong. Not 50%. You'd never notice or even measure this speed increase. Because you optimized a part that take only 0,0001% of whole script runtime.
As for the big HTML tables, it take a long time for the browser to render it. Much more than you took to generate.
Profiling is a key word in the performance world. One can trash any performance related question with no doubts if there is no word "profiling" in it.
At the same time profiling is not a rocket science. It's just measuring of runtime of different parts of your script. Can be done with some profiler, like xdebug, or even manually, using microtime(1). And only after detecting the slowest part, may you start with tests.
Learn to profile before asking performance questions.
And learn not to ask performance questions if there is no real reasons for it.
Premature optimization is the root of all evil - D.Knuth.
I've redone the tests with 50,000 rows and added the multi echo in 1 tag method too
for ($j=0;$j<30;$j++) {
foreach ($results as $key=>$val){
?>
<tr>
<td><?php echo $results[$key][0]?></td>
<td><?php echo $results[$key][1]?></td>
<td><?php echo $results[$key][2]?></td>
<td><?php echo $results[$key][3]?></td>
<td><?php echo $results[$key][4]?></td>
<td><?php echo $results[$key][5]?></td>
<td><?php echo $results[$key][6]?></td>
<td><?php echo $results[$key][7]?></td>
<td><?php echo $results[$key][8]?></td>
<td><?php echo $results[$key][9]?></td>
<td><?php echo $results[$key][10]?></td>
<td><?php echo $results[$key][11]?></td>
<td><?php echo $results[$key][12]?></td>
<td><?php echo $results[$key][13]?></td>
<td><?php echo $results[$key][14]?></td>
</tr>
<?php
}
}
duration1: 31.15542483 Seconds
for ($k=0;$k<30;$k++) {
foreach ($results as $key1=>$val1){
echo
'<tr>
<td>'.$results[$key1][0].'</td>
<td>'.$results[$key1][1].'</td>
<td>'.$results[$key1][2].'</td>
<td>'.$results[$key1][3].'</td>
<td>'.$results[$key1][4].'</td>
<td>'.$results[$key1][5].'</td>
<td>'.$results[$key1][6].'</td>
<td>'.$results[$key1][7].'</td>
<td>'.$results[$key1][8].'</td>
<td>'.$results[$key1][9].'</td>
<td>'.$results[$key1][10].'</td>
<td>'.$results[$key1][11].'</td>
<td>'.$results[$key1][12].'</td>
<td>'.$results[$key1][13].'</td>
<td>'.$results[$key1][14].'</td>
</tr>';
}
}
duration2: 30.23169804 Seconds
for ($l=0;$l<30;$l++) {
foreach ($results as $key2=>$val2){
echo'<tr>';
echo'<td>'.$results[$key2][0].'</td>';
echo'<td>'.$results[$key2][1].'</td>';
echo'<td>'.$results[$key2][2].'</td>';
echo'<td>'.$results[$key2][3].'</td>';
echo'<td>'.$results[$key2][4].'</td>';
echo'<td>'.$results[$key2][5].'</td>';
echo'<td>'.$results[$key2][6].'</td>';
echo'<td>'.$results[$key2][7].'</td>';
echo'<td>'.$results[$key2][8].'</td>';
echo'<td>'.$results[$key2][9].'</td>';
echo'<td>'.$results[$key2][10].'</td>';
echo'<td>'.$results[$key2][11].'</td>';
echo'<td>'.$results[$key2][12].'</td>';
echo'<td>'.$results[$key2][13].'</td>';
echo'<td>'.$results[$key2][14].'</td>';
echo'</tr>';
}
}
duration3: 27.54640007 Seconds
Not much difference between the original 2 methods, but looks like it's quite a bit faster with less concatenation #poke
Since I doubt I'll need this much data in 1 go, I guess I'll continue to use many tags, code indentation looks neater and 'view source' layout more accurate
You can easily ignore the performance difference between those two. With today's modern computing resources, the difference really does not matter. This kind of print-to-screen stuff are truly not to worry about. There are tons of other stuff you should be considering before.
Apart from that, there is always a debate between the best performance and the maintainability of your code. You cannot always try to achieve the best performance. Instead, you should always consider performance concerns along with the amount of time you need to spend on improving them.
Code that is easy to translate to pseudo-code is better. This is evidenced by the examples above. Which takes longer to say?
"Start php, do this 30 times:, then stop php. Print this. Start php, print this, stop php. Print this. Start php, print this, stop php.Print this. Start php, print this, stop php. Print this. Start php, print this, stop php.Print this. Start php, print this, stop php. Print this. Start php, print this, stop php.Print this. Start php, print this, stop php..."
"Start php, do this 30 times: print this, then add this to that, then add this to that, then add this to that, then add this to that, then add this to that, then add this to that..."
"Start php, do this 30 times: print this, print this, print this, print this, print this, print this, print this..."
Personally I would do:
"Start php, define this, do this 30 times: add this to that. Print."
A technical explanation about how the interpreter works and why one way is faster than another is irrelevant for a newbie. It is best just to know the rules of thumb:
Simpler is better.
If it doesn't fit on a single page then it is doing too much (break it down).
If you cannot hand-write the pseudo-code on an index card, it is too complex.
Use more tags if the overall result is simpler. Period.
The real problem with this is memory use. String concatenation and mass echo-ing can increase memory use exponentially.
If you spam the php tag your code becomes unreadable.
Best solution is to use a template engine and avoid mixing code and presentation altogether.

PHP Function vs Echo

I find myself lately working with a ton of tabular data, I am more than comfortable writing the raw html but i'm wondering if there's an easier way (or library worth implementing) that helps reduce time writing html tags such as
<tr><td></td></tr>
I have created my own custom function, but I think ultimately it's not necessarily helping and potentially could be slowing down my script, now my project is small so maybe it could cope with that, examples:
echo '<tr class="test_class">
<td>' . $content . '</td>
<td>' . $second_content . '</td>
<tr/>';
here is an example with my current function:
tr("test_class");
td(); echo $content; escape(td);
td(); echo $second_content; escape(td);
escape(tr);
Looking forward to hearing peoples thoughts.
There are multiple ways of doing this...
write your own html helper library, that will contain classes, that can generate html elements based on their data source. For instance you could call them like:
<?php
HtmlHelper::Table("someArrayOfValues", "idOfTable", "styleOfTable");
?>
This is a good reusable solution, if you implement this idea properly. I was playing with this myself few days ago, really it's simple.
if you find 1. difficult, you can split the idea down... But not so deep like you've shown, but generate whole rows instead.
<?php
foreach ($myArray as $key => $value)
{
echo HtmlHelper::Row(...);
}
?>
Find some library, that provides this functionality. Can't help you on this one I'm afraid. I like to have control over the generated markup.
Hope you get the idea.
If you have short_open_tags turned on (and assuming you can turn it on, if necessary), you can use the templating syntax, like this:
<table>
<?php foreach($myList as $key => $value): ?>
<tr>
<td><?= $value["key1"] ?></td>
<td><?= $value["key2"] ?></td>
...
</tr>
<?php endforeach; ?>
</table>
That might make your job easier, in terms of writing tabular data.
The biggest strength of PHP for web development is how much its made to do with few calls, and in particular for this case the echoing of content without the need to work through language constraints. So in general unless the case is really warranted, directly writing the html with the echoes will be the simplest solution that takes the most advantage of PHP, and simplicity is always a good thing.
That being said, if you have a lot of complex table generation, then the code would be more readable if use a library like: http://pear.php.net/package/HTML_Table/. Additionally if you were looking to do something like serialize an object into a table display, then creating a serializer that is made for that would be the solution most in-line with the functionality.
In the code above I'd suggest a transparent utility function certainly wouldn't hurt. But rather than the direction you're going if you consistently have the same number of columns then you could use an array which is joined with the table cell separation markup (a function that produces a row at a time).
it is more comfort to use some template engines. try twig or smarty

Direct HTML or PHP generated html

What is better / faster:
For example:
STATIC / direct HTML:
<?php
for($i=0;$i<$sth;$i++) {
?>
<tr>
<td>
<?php echo $content; ?>
</td>
</tr>
<?php
}
?>
OR
PHP generated HTML:
<?php
for($i=0;$i<$sth;$i++) {
echo "<tr><td>".$content."</td></tr>";
}
?>
Does it matter which option i choose?
Thanks
It's not so much a matter of speed which may vary based on use case, but of making the code clean and maintainable. Actually both examples make for code that's hard to maintain and read.
I'd suggest using a simple and lightweight templating engine to separate all logic from presentation.
I think that there is no substantial difference between the two, the question should be "Which one is more readable" IMHO and i think that using php and html inline is far less readable than echoing php. But that's just my idea.
The better: Generated html.
Generated html with php is far more easy to maintain and easier to read.
The faster: There is no significant speed difference. However on large dynamic websites where content is loaded from a database etc things might take a fraction of a second more time to output. However, the time you spend on updating a static html file is a lot more than editing dynamic content..
Go dynamic :]
In this case "PHP generated HTML" would be quicker because you are only doing one echo where as in "STATIC / direct HTML" you are doing $sth echos. If $sth is zero then "STATIC / direct HTML" would be quicker.
But seriously, the page is parsed and optimised/normalised so it doesn't make any difference. Parsing with less might be quicker because there are less context switches but this is the smallest part (compared to running it) so it makes negligible difference.
Just pick the style that you feel comfortable with.
Two codes represent the same thing, not differentiate in the speed , But the second code may be a little faster because the code does not contain more than one entry and exit signs.
<?php ?>
this will be carried out faster .
The first approach should be faster as it does not involve a lot of string concatenation. It's also better in terms of code readability.
I think the first solution:
It is clearer and do not require php elaborations with string to dispplay simple static content

Opening/closing tags & performance?

This may be a silly question, but as someone relatively new to PHP, I'm wondering if there are any performance-related issues to frequently opening and closing PHP tags in HTML template code, and if so, what might be best practices in terms of working with PHP tags?
My question is not about the importance/correctness of closing tags, or about which type of code is more readable than another, but rather about how the document gets parsed/executed and what impact it might have on performance.
To illustrate, consider the following two extremes:
Mixing PHP and HTML tags:
<?php echo
'<tr>
<td>'.$variable1.'</td>
<td>'.$variable2.'</td>
<td>'.$variable3.'</td>
<td>'.$variable4.'</td>
<td>'.$variable5.'</td>
</tr>'
?>
// PHP tag opened once
Separating PHP and HTML tags:
<tr>
<td><?php echo $variable1 ?></td>
<td><?php echo $variable2 ?></td>
<td><?php echo $variable3 ?></td>
<td><?php echo $variable4 ?></td>
<td><?php echo $variable5 ?></td>
</tr>
// PHP tag opened five times
Would be interested in hearing some views on this, even if it's just to hear that it makes no difference.
Thanks.
3 simple rules for you to get it right:
No syntax issue can affect performance. Data manipulation does.
Speak of performance only backed with results of profiling.
Premature optimization is the root of all evil
Performance issues are quite hard to understand. It is advised for the newbies not to take it into account. Because they are always impressed with trifle things and fail to see a real important things. Just because lack of experience.
Same for your question. Imagine you'll ever get some difference. Even big one, say, one method is 2 times faster. Oh my, 2 times! I choose it and optimized my app well, it will run 50% faster now!
Wrong. Not 50%. You'd never notice or even measure this speed increase. Because you optimized a part that take only 0,0001% of whole script runtime.
As for the big HTML tables, it take a long time for the browser to render it. Much more than you took to generate.
Profiling is a key word in the performance world. One can trash any performance related question with no doubts if there is no word "profiling" in it.
At the same time profiling is not a rocket science. It's just measuring of runtime of different parts of your script. Can be done with some profiler, like xdebug, or even manually, using microtime(1). And only after detecting the slowest part, may you start with tests.
Learn to profile before asking performance questions.
And learn not to ask performance questions if there is no real reasons for it.
Premature optimization is the root of all evil - D.Knuth.
I've redone the tests with 50,000 rows and added the multi echo in 1 tag method too
for ($j=0;$j<30;$j++) {
foreach ($results as $key=>$val){
?>
<tr>
<td><?php echo $results[$key][0]?></td>
<td><?php echo $results[$key][1]?></td>
<td><?php echo $results[$key][2]?></td>
<td><?php echo $results[$key][3]?></td>
<td><?php echo $results[$key][4]?></td>
<td><?php echo $results[$key][5]?></td>
<td><?php echo $results[$key][6]?></td>
<td><?php echo $results[$key][7]?></td>
<td><?php echo $results[$key][8]?></td>
<td><?php echo $results[$key][9]?></td>
<td><?php echo $results[$key][10]?></td>
<td><?php echo $results[$key][11]?></td>
<td><?php echo $results[$key][12]?></td>
<td><?php echo $results[$key][13]?></td>
<td><?php echo $results[$key][14]?></td>
</tr>
<?php
}
}
duration1: 31.15542483 Seconds
for ($k=0;$k<30;$k++) {
foreach ($results as $key1=>$val1){
echo
'<tr>
<td>'.$results[$key1][0].'</td>
<td>'.$results[$key1][1].'</td>
<td>'.$results[$key1][2].'</td>
<td>'.$results[$key1][3].'</td>
<td>'.$results[$key1][4].'</td>
<td>'.$results[$key1][5].'</td>
<td>'.$results[$key1][6].'</td>
<td>'.$results[$key1][7].'</td>
<td>'.$results[$key1][8].'</td>
<td>'.$results[$key1][9].'</td>
<td>'.$results[$key1][10].'</td>
<td>'.$results[$key1][11].'</td>
<td>'.$results[$key1][12].'</td>
<td>'.$results[$key1][13].'</td>
<td>'.$results[$key1][14].'</td>
</tr>';
}
}
duration2: 30.23169804 Seconds
for ($l=0;$l<30;$l++) {
foreach ($results as $key2=>$val2){
echo'<tr>';
echo'<td>'.$results[$key2][0].'</td>';
echo'<td>'.$results[$key2][1].'</td>';
echo'<td>'.$results[$key2][2].'</td>';
echo'<td>'.$results[$key2][3].'</td>';
echo'<td>'.$results[$key2][4].'</td>';
echo'<td>'.$results[$key2][5].'</td>';
echo'<td>'.$results[$key2][6].'</td>';
echo'<td>'.$results[$key2][7].'</td>';
echo'<td>'.$results[$key2][8].'</td>';
echo'<td>'.$results[$key2][9].'</td>';
echo'<td>'.$results[$key2][10].'</td>';
echo'<td>'.$results[$key2][11].'</td>';
echo'<td>'.$results[$key2][12].'</td>';
echo'<td>'.$results[$key2][13].'</td>';
echo'<td>'.$results[$key2][14].'</td>';
echo'</tr>';
}
}
duration3: 27.54640007 Seconds
Not much difference between the original 2 methods, but looks like it's quite a bit faster with less concatenation #poke
Since I doubt I'll need this much data in 1 go, I guess I'll continue to use many tags, code indentation looks neater and 'view source' layout more accurate
You can easily ignore the performance difference between those two. With today's modern computing resources, the difference really does not matter. This kind of print-to-screen stuff are truly not to worry about. There are tons of other stuff you should be considering before.
Apart from that, there is always a debate between the best performance and the maintainability of your code. You cannot always try to achieve the best performance. Instead, you should always consider performance concerns along with the amount of time you need to spend on improving them.
Code that is easy to translate to pseudo-code is better. This is evidenced by the examples above. Which takes longer to say?
"Start php, do this 30 times:, then stop php. Print this. Start php, print this, stop php. Print this. Start php, print this, stop php.Print this. Start php, print this, stop php. Print this. Start php, print this, stop php.Print this. Start php, print this, stop php. Print this. Start php, print this, stop php.Print this. Start php, print this, stop php..."
"Start php, do this 30 times: print this, then add this to that, then add this to that, then add this to that, then add this to that, then add this to that, then add this to that..."
"Start php, do this 30 times: print this, print this, print this, print this, print this, print this, print this..."
Personally I would do:
"Start php, define this, do this 30 times: add this to that. Print."
A technical explanation about how the interpreter works and why one way is faster than another is irrelevant for a newbie. It is best just to know the rules of thumb:
Simpler is better.
If it doesn't fit on a single page then it is doing too much (break it down).
If you cannot hand-write the pseudo-code on an index card, it is too complex.
Use more tags if the overall result is simpler. Period.
The real problem with this is memory use. String concatenation and mass echo-ing can increase memory use exponentially.
If you spam the php tag your code becomes unreadable.
Best solution is to use a template engine and avoid mixing code and presentation altogether.

Opensource project's PHP syntax

When working with open source project (like wordpress, drupal, joomla) I always find in the PHP pages a syntax like (this is an example from drupal):
<?php if ($linked_site_logo or $linked_site_name): ?>
<?php if ($title): ?>
<div class="logo-site-name"><strong>
<?php if ($linked_site_logo): ?><span id="logo"><?php print $linked_site_logo; ?></span><?php endif; ?>
<?php if ($linked_site_name): ?><span id="site-name"><?php print $linked_site_name; ?></span><?php endif; ?>
</strong></div>
<?php else: /* Use h1 when the content title is empty */ ?>
<h1 class="logo-site-name">
<?php if ($linked_site_logo): ?><span id="logo"><?php print $linked_site_logo; ?></span><?php endif; ?>
<?php if ($linked_site_name): ?><span id="site-name"><?php print $linked_site_name; ?></span><?php endif; ?>
</h1>
<?php endif; ?>
<?php endif; ?>
while I do use a different syntax writing my scripts; if I did wrote the previous example it would look something like:
<?php
if($linked_site_logo or $linked_site_name){
if($title){
echo '<div class="logo-site-name"><strong>';
if($linked_site_logo){ echo '<span id="logo">' . $linked_site_logo . '</span>'; }
if($linked_site_name){ echo '<span id="site-name">' . $linked_site_name . '</span>'; }
echo '</strong></div>';
}else{ /* Use h1 when the content title is empty */
echo '<h1 class="logo-site-name">';
if($linked_site_logo){ echo '<span id="logo">' . $linked_site_logo . '</span>'; }
if($linked_site_name){ echo '<span id="site-name">' . $linked_site_name . '</span>'; }
echo '</h1>';
}
}
?>
Now, lets skip the 'appareance' of the 2 syntax methods, becose it is maybe a matter of taste and/or custom (obviously I prefer the second method), the question is:
Does the first syntax (breakinf the 'if' statements, output the HTML instead of echo it, have a lot of PHP snippets even if they arent really needed) have some technical advantages over the second one? (for example the script run faster, is easier to debug, etc...)
Or is just a open source programmers unwrited convention?
It's all about readability.
I don't know what you mean by output vs echo. There is no difference. They're just different ways of printing "stuff" to output that is sent to the client.
The disadvantage of:
echo "<div id=\"blah\">";
is twofold:
The extra slashes require effort to put in and make it less readable; and
HTML outside PHP code blocks will syntax highlighted by most PHP editors.
I wouldn't go as far as saying echoing HTML is evil in all cases, but it certainly has a lot of drawbacks. In addition to what cletus points out, your HTML is not structured anymore, i.e. the indention levels give you no indication of where you are in the document structure. That's a biggie for me.
Personally, I dislike the first style as well, as it makes the PHP code harder to read. I always try to strike a balance, multi-line PHP statements belong in one <?php ?> block, but HTML always belongs outside the <?php ?> block. In edge cases, e.g. when indention levels change inside the PHP block, I tend towards closing it and beginning a new block.
I can see that that opens up a can of worms regarding edge cases and when to use which, so I'm sympathetic to open source projects setting a formal rule to always close <?php ?> blocks.
The biggest "advantage" I could see to the former method would be that it's easier to insert HTML anywhere within the overall control flow - if you wanted to output some HTML before the if($title) check, you could just insert a line above it with the HTML, no need to escape things for an echo or whatnot.
afaik The reason for this is that graphic designers can edit the HTML in their tools (dreamweaver and similar). Those tools would show the php tags as just that or even hide them completely. That way they can design away without touching your code, which is, believe me, a massive advantage when collaborating with designers.
Actually they are not the same. in fact in your second example, php interpreter will do unnecessary step, which is printing out html elements. thus resulting poor performance depending on the size of the page. checout google's article "lets make web faster" http://code.google.com/speed/articles/optimizing-php.html.
They are the same. I suggest you stick what you have been used to do because that is more readable to you.
If you follow MVC - you have the view and model (domain logic) parts. For the view you use the first method because it's HTML with tiny PHP parts in it, and for the model you use the second method - it's pure PHP anyway. It's a very common approach afaik.
Examples:
Zend Framework - see zend view manual
WordPress - the code (even messy parts) are method 2, and the themes are method 1
Keeping one hierarchy of consistent indentation for both code and markup is essential for coping with complex templates. In the first example I can immediately see the structure of the tags; the second makes me work to understand what's going on. Without reading through it I can't see whether it's doing something like leaving an element open. IMO PHP should be written like XHTML, as if the ‘if’ statements were tags you had to balance.
(Personally though I prefer the standard {...} syntax to the alternative :...endif one. I don't see what advantage that brings.)
Legend has it that direct PHP-templated output is marginally faster than echoing everything. But if there's really any difference it's too small for me to measure. Certainly compared to any other work your script will be doing, it's inconsequential. t's only the readability that really matters. PHP is a templating language, though — you might as well take advantage of it!
[both examples fail to htmlspecialchars, tsk.]

Categories