Open and close the code or use variable?

5

Two scripts with the same result:

Example 1:

<?php

...códigos PHP acima...

$tabela = '<table>';
$tabela .= '<tr>';
$tabela .= '<th> Valor1 </th>';
$tabela .= '<th> Valor2 </th>';
$tabela .= '<th> Valor3 </th>';
$tabela .= '</tr>';
$tabela .= '<tr>';
$tabela .= '<td>' . $valor1 . '</td>';
$tabela .= '<td>' . $valor2 . '</td>';
$tabela .= '<td>' . $valor3 . '</td>';
$tabela .= '</tr>';
$tabela .= '</table>';

echo $tabela;

...códigos PHP abaixo...

?>

Example 2:

<?php

...códigos PHP acima...
?>

<table>
  <tr>
    <th> Valor1 </th>
    <th> Valor2 </th>
    <th> Valor3 </th>
  </tr>
  <tr>
    <td> <?= $valor1 ?> </td>
    <td> <?= $valor2 ?> </td>
    <td> <?= $valor3 ?> </td>
  </tr>
</table>

<?
...códigos PHP abaixo...

?>

I would like to know if there are, and what would be, the advantages and disadvantages of the examples above.

For example : I think that handling everything in a variable for later printing would take up more memory and processing, as there is concatenation and space in memory (especially if we consider large loops) p>

Point that raised the question:

Overflow error

  

Fatal error: Allowed memory size of 134217728 bytes exhausted   (tried to allocate 36 bytes)

Crossing values in array

    
asked by anonymous 25.07.2018 / 17:54

2 answers

6

The first involves more processing, is a PHP code being interpreted, processed to generate text that will be sent to the server (possibly). The second is already considered the text to be sent only by invoking the PHP processor in some parts, and has a much lower cost.

So the first one has the cost of the buffer of the HTML text as a whole and the buffer of the string manipulation. >

In general the second is more readable. The second is usually used more like a template page to render where only a few gaps are filled by rendering, which gives a lot more work to run since everything needs to be guaranteed correct, unlike the HTML that can be sent to all wrong that is the client's responsibility, possibly a web browser, to determine if it is right or not.

The first is not common and should only be used in cases where it is something very complex to process. It would usually be better to do the rendering and then generate the HTML text.

It tends to use more memory also because it will occupy in PHP to process as data and then create the final text. But that's implementation detail.

Even PHP having mutable strings still tends to allocate more by doing several concatenations like this. It is not as critical as languages that work with immutable strings.

In this specific example I would opt for the second one (I had a different era, but I was wrong, I did it for pleasure and not for necessity). There are cases that I could opt for the first. Not that it will make a huge difference. I always say that if you want efficiency choose another language, PHP gives you ease (or give), not efficiency.

Just notice that I'm cherishing things a bit. I can see a scenario that the former can be more efficient. It should not, but depending on the PHP architecture is possible, I just would not count on it. Either way would have to evaluate, but again, if necessary, the tool is wrong.

This memory error should be something else. This alone does not cause this kind of error, but several other things wrong together can cause the error in any minimal allocation. It's just a coincidence occurring there, for this case it does not do tickle .

    
25.07.2018 / 18:05
4

There is no right and wrong here , there are needs, at first you saved everything in a string and then threw it to the echo, this is useful if you treat the value of the variable including HTML

Now if the intention is to do nothing with value of the variable and only to play to echo the second example is much easier to maintain and understand your own HTML, even if it was a while loop that would bring results and an array or database.

So to conclude, you can use either, but the first one will be useful even if you manipulate $tabela , DOMDocument , or string functions like, substr , str_replace , preg_replace , etc.

Now on memory, the probability of this occurring is very rare, if it occurred it is because it did something very absurd, serious even and could even occur without the variable, for example if using the buffer, as ob_start (or even without buffer), I must say that if it occurred it is because you did something very wrong, assuming your php.ini limit to memory_limit=2MB , string would have to be giant even to achieve this.

Other situations that can cause this is the manipulation of images with GD, which is complex and really consumes a lot of memory, the use of heavy frameworks (I will not mention names, in chat who have interest on the subject of frameworks can call me ) or a loop that keeps creating variables in any way without any control.

To go to the end, the problem with memory is usually in the form of how it was done and not necessarily in variable selection or direct display.

    
25.07.2018 / 18:06