Hello, looked at the performances zendovtsa , read articles on Habré . The field of what appeared is a logical, but philosophical question about code optimization.

Now

echo 'Hello', $username, '!'; 

slower than

 echo 'Hello, {$username}!'; // вероятно, фигурные — лишние 

Now something like

 for ($i = 0, $l = count($arr); $i < $l; $i++) { $str = 'Hello, World'; } 

Equivalently:

 $str = 'Hello, World'; for ($i = 0; $i < count($arr); $i++) {} 

Because the string is immutable and the interpreter knows this. That is, you will write unset($str) at the end of this block, how it will be slower to work ... It is strange to me. Explain, please, supporting sources how now to write the code more optimally? Where to write unset.

1 answer 1

Perhaps you meant echo "Hello, {$ username}!" not echo 'Hello, {$ username}!

As for unset (), this construction clears the memory allocated for storing the variable data, as well as the memory allocated for the variable name. If several assignments to the same variable are planned, it is natural that unset between these assignments will perform an extra operation. https://habrahabr.ru/post/134784/

As for the new optimization, like "Unchangeable arrays": in the particular case - if the variable is unchanged or the interpreter can determine that there are no changes, it will be able to use the value of this variable in different parts of the code with the lowest memory cost. The unset () variable can be interpreted by the interpreter as changing such a variable and using its value in different fragments of the executable code will be impossible. https://habrahabr.ru/company/mailru/blog/318008/