Does the code need to delete all used variables at the end? For example:

$test = date("dmY"); echo $test; unset($test); // Нужна ли эта строка? 
  • one
    It looks like Paranoid Style Coding :) PHP has the Garbage Collector, you can even then run it. - Shamanis
  • @Shamanis: "Looks like Paranoid Style Coding" Yes, there is a bit) Garbage Collector - start it at the beginning, then at the end you start gc_disable () and all the garbage is deleted? But is it necessary? Probably it is necessary rather for PHP win32 window attachments, so that the RAM will not be littered? - iproger
  • Php win32 window attachment? Xv - BomBom
  • Well, yes, the PHP GUI is called. I never did for Windows applications on php? - iproger
  • Judging by the example of the question from the theoretical section, respectively, there is a good article on this topic: [Working with memory (and yet it is)] [1] [1]: habrahabr.ru/blogs/php/134784 - chernomyrdin

4 answers 4

It all depends on what you are doing next and have done before. If you create a simple object (or variable) - then the meaning of it to destroy? The garbage collector will do everything himself. If you are creating a voluminous object now and further, you also plan to create something bulky (for example, in a cycle) and at the same time fear that your memory will become a girl (sorry girls, you suffer from it), then you can also call the collector. You can read more about destructors. They write little about them, because they are rarely used, but what if this is the case?))

    Still, if this variable is used in some part of the code, which is a separate logical block, this block can be formed as a function with its local variables.

    When the function runs, all its local variables will free up memory.

      It is necessary to clean it only if the variable with the volume of information contained in it exceeds 100kb and more, although there is already your choice, I am from personal experience. It happens that during the execution of the script can eat and 300kb, or even 2MB. But your code can be simplified:

       echo date("dmY"); 

      If the received date is not needed anywhere else.

      • Thank you, "But your code can be simplified:" - CEP =) - iproger
      • I am glad to try)) - Andrey Arshinov

      Not necessary.

      • @Elime, Explain your answer. - Nicolas Chabanovsky
      • @Expert, There is no need to release the variable memory, php itself frees the memory. If the variable is passed through global arrays, then it makes sense to kill it. Removing variables for the sake of script speed is baytodrochestvo, which should not bother. IMHO. - Elime