PDA

View Full Version : set and unset variables


alaios
05-16-2005, 02:55 PM
Hi i know that i can use any variable without declaring it first.... but i want to know what is the overhead for that? Is it better to declaring a variable before using it? var $myname;
Also do u know if i can unset the variable that i dont use? Is it worth doing that? I use a variable only for 2 or three lines of code... Should i unset it?
Thx a lot

I

delinear
05-16-2005, 04:32 PM
As far as I know there's no difference between declaring a variable in advance and creating a variable by setting its value, although with the advent of PHP 5 it's good practice to declare variables.

As for unsetting variables, of course it's good housecleaning to unset them but if they only contain minimal data you're unlikely to see any performace increase by doing so, it's just additional effort on your part. Personally I don't unset variables unless they're holding large quantities of data which is no longer needed or unless I have a specific reason for unsetting a variable.

Fou-Lu
05-17-2005, 01:57 AM
I'm also with delinear on the unsetting, if its not nessessary to do, I never bother with it. I would however initialize all of your variables yourself, especially if your register_globals are on, which would really be bad. If this were the case and you rely on the setting of variables automatically, a client could just as easily pass this variable via a get method and throw your entire script into chaos. Which of course, would be a nightmare :P