Open and close the code or use variable?

Asked

Viewed 268 times

3

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, the advantages and disadvantages among the above examples.

For example: I think to treat everything in a variable to then print, would occupy more memory and processing, because there is concatenation and space in memory (especially if we consider in large ties).


Point that raised the question:

Error due to over-calculation

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

Cross values in array

  • Downvoter, comment on why -1, so I can improve the question.

  • To think: and you think that in the second example the HTML will be stored where, since in the first one would be in memory?

  • I think I expressed myself badly. It would be the case of the memory of the variable, which exhausts: example Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes). Were the two forms equal? Now confused me!

2 answers

5


The first involves more processing, is a PHP code being interpreted and processed to generate a 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 (to process the text).

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

Generally the second is more readable. The second is usually used more as a template of the page to be rendered where only a few gaps are filled by processing, which takes much more work to execute since everything needs to be guaranteed to be correct, unlike the HTML that can be sent up every wrong that is the responsibility of the client, possibly a web browser, determine whether it is right or not.

The first is not common and should only be used in cases that are very complex to be processed. Generally it would be better to do the processing and then generate the HTML text.

It tends to use more memory also because it will take up in PHP to process as data and then create the final text. But this is implementation detail.

Even though PHP has strings changeable yet tends to allocate more by making multiple concatenations like this. It’s not as critical as languages working with strings immutable.

In this specific example I would choose the second one (there was a time when I did it differently, but I was wrong, I did it out of taste and not out of necessity). There are cases I could opt for first. Not that it will make a huge difference. I always say that if you want efficiency opt for another language, PHP gives you ease (or gave), not efficiency.

Just note that I’m estimating things a little. I can see a scenario that the former can be more efficient. It shouldn’t, but depending on the architecture of PHP is possible, I just wouldn’t count on it. Anyway I would have to evaluate, but again, if necessary, the tool is wrong.

This memory error must be about something else. This in itself does not cause this kind of error, but several other wrong things together can cause the error in any minimal allocation. It’s just a coincidence to happen there, for this case does not even tickle.

  • "It tends to use more memory also because it will take up in PHP to process as data and then create the final text."... You say that when I "break" the script (?><?) I am cutting and starting again the PHP process or not? For example: in the example1 it processes everything in 1x, in the example2, it processes 2x?

  • 1

    @RBZ What comes after a ?> goes to the php output buffer (therefore also occupies memory). Inside the php code, it only goes to the buffer what you say to give echo.

  • @bfavaretto boy, now made me more complicated. I’ll have to go deeper on this. I realized that the little I had idea, is little more than I thought! rs But thank you!

  • 2

    @RBZ is a bigger concern than it is a problem.

4

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

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

So to conclude, you can use either, but the first will be useful even if you are manipulating the $tabela, with DOMDocument, or with string functions such as, substr, str_replace, preg_replace, etc..

Now about memory, the probability of this occurring is very rare, if it occurred is because it did something very absurd, very serious and could occur even 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 it did something very wrong, assuming that its php.ini limit for memory_limit=2MB, to string I’d have to be a giant 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 name names, in chat who is interested on the subject of frameworks can call me) or a loop that keeps creating variables in a way without any control.

To get to the end, the problem with memory is usually in the form of how it did and not necessarily in set variable or direct display.

  • I hadn’t really thought about it, the manipulations in string, and even the DOM in case you need to print. It is that the question arose in case of exceeding the limit, as I edited and posted at the end. But then again, it would be the same space.

  • 1

    @RBZ to exceed the memory you have to have done something giant, and it can occur even if it is not string, it has to be something absurd even, what usually exceeds memory is manipulation of a lot of data without care, from which it generates innumerable variables, or manipulate complex data, as images with GD.

  • I get it. So in these cases they would be totally irrelevant.

  • @RBZ edited the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.