Imagine that we use the MVC approach and pass a certain array of data to the view. These data, as far as I know, can be displayed in at least two ways:
one)
<div class="content"> foreach($array as $value){ echo ' <div class="box"> <div class="title">'.$value['title'].'</div> <div class="content">'.$value['content'].'</div> </div>'; } </div>
2)
$content = ''; foreach($array as $value){ $content .= ' <div class="box"> <div class="title">'.$value['title'].'</div> <div class="content">'.$value['content'].'</div> </div>'; } <div class="content"><?=$content ?></div>
So, which of these methods of forming content is more correct? As far as I know, the echo operator during each call increases the script's running time (at least in the console), is there a faster way to use the second method, where echo is called only once. And how is it generally accepted to write in projects?
UPD: Checked the speed of the two output options. The results are more than impressive. The difference in speed is about 100 times. Here is the script itself if that.
<?php $start = microtime(1); for($i = 0; $i<1000; $i++){ ?> <?= '+'; ?> <?php } $finish = microtime(1)-$start; $start = microtime(1); $content = ''; for($i = 0; $i<1000; $i++){ $content .= '-'; } echo $content; $finish2 = microtime(1)-$start; echo 'Время выполнения через echo: '.$finish.'сек '; echo 'Время выполнения через конкатенацию: '.$finish2.'сек';