Page 1 of 1
Reusing Variables
Posted: Sun Apr 18, 2010 10:45 pm
by JakeJ
In my application, I reuse a lot of my variables. In particular I reuse $txt a lot because it seems to me, that it's a lot easier to reuse it than it would be to create an insane amount of variables.
What I don't know is, is it more efficient in the application/memory sense to reuse the same variable or or declare one for each text string I need to use? Maybe to put it more simply, where is there more overhead, overwriting a variable or instantiating a new one?
Re: Reusing Variables
Posted: Sun Apr 18, 2010 11:00 pm
by Benjamin
This is actually the least of your worries. DB queries, classes, included files and memory usage impact performance an order of magnitudes more than variable usage.
Re: Reusing Variables
Posted: Mon Apr 19, 2010 7:50 am
by marty pain
You get less overhead by re-using variables, but it is minimal so do what ever is easier for you and other people to understand.
Re: Reusing Variables
Posted: Mon Apr 19, 2010 10:38 am
by JakeJ
In this case, reusing the variable is the easiest and most readable since it's getting stuck inside a function anyway. I suspected it was also the best utilization of memory, etc. also but things are not always as they appear so I thought I'd check but I really wasn't willing to take the time to do benchmarking to figure it out.
Re: Reusing Variables
Posted: Fri Apr 23, 2010 6:05 am
by siji86
Reusing the variable is perfectly ok, but it makes the code difficult to understand for example, if i have used customer_id somewhere and later on i use customer_id to store a email id then this seems a bit confusing. So according to me its better to reuse variables only when neccessary.
Re: Reusing Variables
Posted: Fri Apr 23, 2010 1:35 pm
by JakeJ
In this case, I'm just reusing a variable called $txt which as you might guess is text. Not likely to cause confusion in this case but I take your point.