Jump to content

when to unset a variable?


ollie007

Recommended Posts

All resources used in the php code on any page get destroyed when that page ends execution. Within the php code on any page you can reuse variables any way you like. You would only specifically unset them if you needed to free up the memory used by them or if your application needed your to unset them for logical reasons later in the code on that page.

Link to comment
Share on other sites

as PFM says, all data is destroyed when the script exits, minus sessions, they are stored in a file somewhere on teh computer with the unique session ID for that session.

 

Variables arn't exactly 'floating' around, they're exactly where you need them, there are two main reasons you'd unset a variable,

 

1) You have an if statement which checks for 'isset($whatever)' and you could have a certain procedure to check whether or not to actually unset $whatever or not due to other factors, but that is bad practice, you should use booleans for that.

 

2) You are handling alot of data, and to avoid your script producing an error and dying, you should free up memory as you progress in the script

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.