I have a sytem where I want to build an HTML table in PHP from data retrieved from a database.
Earlier, I used two different methods for creating HTML and repeating it.
Building the returned variable and echo at the end of the PHP script:
<?php
$data['category']['parts'];
$retval = '<table>';
foreach($data as $category) {
$retval .= '<tr>';
foreach($category as $data) {
$retval .= '<td>'.$data.'</td>'
}
$retval .= '</tr>';
}
$retval .= '</table>';
echo $retval;
Another method is to echo each line when the code arrives at it:
<?php
$data['category']['parts'];
echo '<table>';
foreach($data as $category) {
echo '<tr>';
foreach($category as $data) {
echo '<td>'.$data.'</td>'
}
echo '</tr>';
}
echo '</table>';
Which of the two methods is more efficient in terms of CPU / memory usage, as well as for processing speed? Is there a real difference, not just a matter of style?
source
share