Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm fairly new to PHP and have a few questions.
What I'm not sure of is how I should structure or implement PHP code into the HTML.
So far, I have structured the coding in two different ways, either echoing out the HTML or keep the PHP as minimal as possible.
What of the two following examples would you prefer? I guess most would answer code number one, but why? Is it because of the abillity to read the HTML syntax highlights?
As you can see in code number one I have to type the PHP start and end tags for just ending the foreach function, which is a bit annoying when the code starts to get really big. Looks like the code is full of start and end PHP tags for the smallest operation.
How do you structure your code?
Code example 1:
<table border="5">
<?php
foreach ($a3 as $nokkel => $verdi){
?>
<tr>
<td><?php echo $nokkel; ?></td>
<td><?php echo $verdi['selskap']; ?></td>
<td><?php echo $verdi['siste']; ?></td>
</tr>
<?php } ?>
</table>
Code example 2:
<table border="5">
<?php
foreach ($a3 as $nokkel => $verdi){
echo "<tr>";
echo "<td>$nokkel</td>";
echo "<td>$verdi['selskap']</td>";
echo "<td>$verdi['siste']</td>";
echo "</tr>";
}
?>
</table>
Both examples are good. But i usually prefer example 2.
But in minimal coding like this ( No need to echo again and again ):
<table border="5">
<?php
foreach ($a3 as $nokkel => $verdi){
echo '<tr>
<td>'.$nokkel.'</td>
<td>'.$verdi['selskap'].'</td>
<td>'.$verdi['siste'].'</td>
</tr>';
}
?>
</table>
It depends on how much HTML content and PHP variables, you have on the page.
Considering your example, it's a simple table, with just values to be displayed. <%= $some_value %> vs echo "$some_value" isn't much of a difference.
But incase of complex HTML pages, where there's a lot of content, I believe the Example 1, is going to come in handy. Embedding PHP here and there instead of echoing the content, is more convenient, readable and future editable.
This question and the answers will be highly opinionated, since you're asking what people prefer.
However, I'd like to point out that php does support a different syntax than the one you're using, namely foreach:/endforeach if/endif, etc. This way, your example
<table border="5">
<?php
foreach ($a3 as $nokkel => $verdi){
?>
<tr>
<td><?php echo $nokkel; ?></td>
<td><?php echo $verdi['selskap']; ?></td>
<td><?php echo $verdi['siste']; ?></td>
</tr>
<?php } ?>
</table>
could be better written like this:
<table border="5">
<?php foreach ($a3 as $nokkel => $verdi): ?>
<tr>
<td><?php echo $nokkel; ?></td>
<td><?php echo $verdi['selskap']; ?></td>
<td><?php echo $verdi['siste']; ?></td>
</tr>
<?php endforeach; ?>
</table>
This has several added benefits:
it's more compact (wastes fewer lines)
it's easier to follow several block levels (think if's inside if's inside foreach's)
it's easier to read
you keep indentation in your code. This is important, since with your echo suggestion, you quickly lose a good overview of which html indentation level you are on.
Syntax highlighting will still be applied in all your examples if you have a decent IDE, so that shouldn't be a major concern.
Edit: Added a link to the php manual for the alternate control syntax.
Edit 2: Since we're into the topic of alternate syntaxes, you can also utilize the "short echo" syntax to further reduce the verbosity of your code, by changing your <?php echo to <?=. Short echo is always available if you're using php version 5.4 or later (which you should most definitely do if you're starting to learn or use php now).
Using this, your code could be further refactored into this:
<table border="5">
<?php foreach ($a3 as $nokkel => $verdi): ?>
<tr>
<td><?= $nokkel; ?></td>
<td><?= $verdi['selskap']; ?></td>
<td><?= $verdi['siste']; ?></td>
</tr>
<?php endforeach; ?>
</table>
The two examples are okay and it really depends on the developer which one he/she thinks has better readability. But whatever you choose, there are always trade offs like for example:
for example 1, you still have to type lots of php starting and ending tags. While for example 2, you still have to write php tags once though with every html tags must be inside an echo. But for me, Option 1 has more readability for me than the second one so I choose option 1.
Generally when following the Model-View-Controller pattern, which is often done in web applications now-a-days, developers try to keep as much logic as possible outside the HTML generation.
That's why most web development frameworks use a template engine, which lead to code much closer to your first example. Aside from the advantages you and the others mentioned such as clearer syntax, one big advantage is that these engines make sure that the output is cleanly escaped. In pure PHP you have to remember to use htmlspecialchars making the code even longer - and if you forget it can result in dangerous security holes.
So my advice is: Don't use either syntax. Instead look for a template engine to use instead.
Related
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.
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
Whenever I move the codeblock that generates courses with a grade of "F", it does not not echo out the courses that meets the criteria. But when I move it to beginning of the main div header of the script it displays well.. What could the problem be? I actually want it to be at the end of the script for the sake of printing out. I've also checked the source code and the parameters I was looking for wasn't there.
Pastebin of full code
echo "<table bgcolor = red >";
echo "<tr align= \"center\">";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<th>"."Failed Courses : "."</th>";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo"<td>". $row8['course_code']."</td>;
}
}
echo "</tr>\n";
echo "</table>";
There are inconsistencies between your provided example here and the pastebin code you linked to. Which version are you using?
If it's the pastebin version you may be having issues with this line:
echo "<th>"."Failed Courses";
You are missing the </th> tag which could be messing with your output.
It seems like it is going to be a long bit of code so I'm posting it here, I don't intend for this to be an answer to your problem as I can't get it to do what you're saying.
PHP allows for information to be set outside of the PHP tags, and when that happens it is treated as regular HTML.
E.G.
<?php
If(2==2){
?><b>HELLO!</b><?php
}
?>
And
<?php
if(2==2){
echo "<b>HELLO!</b>";
}
?>
Will both result in <b>HELLO!</b> to be output to the screen. This is useful for things that might require a lot of HTML in more than one block, as well as HTML that may require extensive style definitions or other places where double quotes will be needed, for example
<div id="list_row[$i]" class="something something2 something3"> would have to be escaped as
echo <div id=\"list_row[$i]\" class=\"something something2 something3\"> whereas it could just be put more or less intact in php using the above mentioned fact. Now, there would still need to be an echo statement for the $i portion, as I don't think PHP processes the text, but I've never tried it so I can't be sure.
As for your HTML,
<th> tags are meant to be a header, aka in the top part of a table. <td> are meant to be table-data cells.
Seems like you're making a lot of unnecessary calls to the same MySQL tables, and I'm hoping I can condense it down a bit.
Why are you doing this?
echo "<th>"."Failed Courses : "."</th>";
do
echo "<th>"."Failed Courses : </th>";
and also before you do a while loop try to see what your results looks like.
$row8 = mysql_fetch_assoc($query8)
then print_r($row8);
post your results.
Also the table structure should be
<table>
<th></th>
<tr>
<td></td>
</tr>
</table>
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"
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.