This is just a tip for when your building your applications in PHP and why you should use the unset() function.

For unset there are 2 main reason why you would use such a command.

  1. To unset a variable so you can redeclare it later as a fresh variable!
  2. Reduce memory usage dure the execution of your script


Now the main problem in PHP scripts is keeping large variables throught the whole execution of the script, causing the script to slow down dramatically after the variable is set..

here is an example of not using unset and the ram levels changing.

PHP Code: 
//First we check the state of the memory at the start.

echo memory_get_peak_usage() . "\n"// 36640

/*
    * as you can see above the ram usage is at 36640.. 36.6Mb
    * but dont forget the 36.6Mb is also apache,mysql and other services
*/

//Lets run a test to see if we can get it to peak

$a str_repeat("KWWHunction"4000); // repeat a string 4000 times


//Lets check what effect that above has had on the memory usage
echo memory_get_peak_usage() . "\n"// 60765

/*
    * As you can see the memory is 60.7Mb thats 24.1Mb increase
*/ 
Now if you carry on coding the memory usage will just grow as the variable $ is still in the memory waiting to be used again...

but if theres no use for $a, so lets take a look at the effect if we unset the variable after we have finished with it..

PHP Code: 
//First we check the state of the memory at the start.

echo memory_get_peak_usage() . "\n"// 36640

/*
    * as you can see above the ram usage is at 36640.. 36.6Mb
    * but dont forget the 36.6Mb is also apache,mysql and other services
*/

//Lets run a test to see if we can get it to peak

$a str_repeat("KWWHunction"4000); // repeat a string 4000 times

//UNSET HERE
unset($a);

//Lets check what effect that above has had on the memory usage
echo memory_get_peak_usage() . "\n"// 38230

/*
    * instead of a 24.1Mb increase we only have a 0.2Mb increase
*/ 
I hope you can see the importance of unset() and how it will help you make your site load a HELL of a lot faster, especially if your new to programming in PHP and you do stuff with code when you don't fully understand what it does to your memory usage.

Peace out
litewarez Reviewed by litewarez on . PHP TIP: Unset() : Memory Usage! This is just a tip for when your building your applications in PHP and why you should use the unset() function. For unset there are 2 main reason why you would use such a command. To unset a variable so you can redeclare it later as a fresh variable! Reduce memory usage dure the execution of your script Now the main problem in PHP scripts is keeping large variables throught the whole execution of the script, causing the script to slow down dramatically after the variable is set.. Rating: 5