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.'сек'; 
  • The first is easier to debug, the second for mazahist perverts. - Opalosolo
  • With an error, the second method was originally written by me. Now still for perverts?) And if so, please argue. - makbeth
  • one
    @makbeth - I apologize, I wrote from the phone and did not see echo there - these are both options for debugging in the IDE is almost unreal. Use the method that @andreyqin suggested to you - this is the best you can do. - Opalosolo

2 answers 2

Alternatively, make php-inserts in the markup or use (write your own) template engine:

 <div class="content"> <?php foreach ($array as $value) { ?> <div class="box"> <div class="title"><?php echo $value['title']; ?></div> <div class="content"><?php echo $value['content']; ?></div> </div>'; <?php } ?> </div> 
  • If you use this method, then echo calls will be at least twice as large. And this will affect the speed. Or do I not know something? - makbeth
  • 3
    This is saving on matches - andreyqin
  • I always do it myself - anunak
  • In general, I understand the opinion. I will continue to use this method. Thank! - makbeth
  • @makbeth - here’s how you can cut the code: <div class = "title"> <? = $ value ['title']?> </ div> <div class = "content"> <? = $ value [' content ']?> </ div> - Opalosolo

If, as in the first version, to use echo, then these are unnecessary function calls and work with the stack of parameters. If, as in the second case, concatenates small values ​​into one variable, then these are redundant allocations of memory in the common heap.

Option with PHP code inserts

 <div class="title"><?php echo $value['title']?></div> 

optimally, because the number of echo is less, and to accumulate the result, an output buffer is used, to which memory is already allocated.

In support of my words, I will give the following simplified code that is used in frameworks using the template engine of the "pure PHP" class:

The View class is a View component for the MVC design pattern — it passes the user-defined parameters $ vars to the template as a variable in the local scope. The view.php file is a template, that is, a PHP file, in which a fragment of HTML code is directly created.

 <?php class View { function render($vars) { extract($vars); // перевести массив в набор локальных переменных ob_start(); //открыть буфер include('view.php'); return ob_get_clean(); // закрыть буфер и вернуть его содержимое } } // где то в другом месте делается так $view = new View; echo $view->render(array('array' => array('title'=>'title1', 'content'=>'content1')); ?> 

As you can see, the render method uses a buffer.

In general, in the frameworks of such calls there are hundreds on the code and the saving of both time and memory is significant. If you consider not one call, but hundreds per second for a day, the server will load the processor less and require less RAM, which means you can either use a weaker and cheaper server, or serve more requests per second and please with a higher response rate site visitors.

Is the meaning clear?

  • Not really. Suppose we call the render method. It takes an array of data that we need to display in the template. Display how are we going? Through array search most likely. Again, the same question seldom. Or maybe I do not understand? By the way about the speed ... I measured it here just now two options for output ... Attach the finished script to your question. If you run it for example in the console, you will see a difference in the speed of work of about 100 !!! time. - makbeth