I don't think you ever have to be concerned about memory leaks with PHP.
Memory leaks occur when your program reserves memory to dynamically create an object in your program, and then all of the references to that object are destroyed without formally releasing the memory again. The operating system, e.g. Windows, will continue to see this memory as being reserved and will not be able to use it again until you reboot your computer. Since all of the references to the object in your program have been destroyed, your program can no longer release this memory.
Dynamic objects are created by allocating memory on what is called the heap, and the variable in your program is just a reference to that object, it just points to it. If you then break the connection between that reference and the object's memory that it is pointing to, without releasing (unreserving) the memory, you get a memory leak. If this keeps happening again and again in your program, at some point your operating system runs out of available memory because everything is reserved for these no longer existing objects.
Here an example:
-
// something maybe from C++
-
MyClass myObject = new MyClass();
-
...
-
// reuse the reference myObject for another object
-
myObject = new MyClass();
-
Its been a long time since I programmed C++, so maybe I don't get it all right. But the idea is that I created a reference myObject that points to a new object of my class MyClass, and this allocates memory on the heap. Later, I forget to "destroy" (tell the code to unreserve) the object, and then reuse the reference myObject in creating a new object and reserving more memory. The original memory is now reserved and unusable for anyone.
This is what a memory leak is basically all about.
However, unless I am wrong, this is nothing you need to worry about with PHP. When a script is finished running, I believe the PHP interpreter releases all memory and all system resources that the script has used, it does this for you automatically.