Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it matter if I use 1 php variable multiple times for different purposes?

Tags:

php

I'm using $query, $row, etc.. variables in my code multiple time, and assigning different things to them. I wonder if it's matter or wrong coding style? Does it affect to website or server performance anyway?

like image 306
Tural Ali Avatar asked Aug 29 '11 15:08

Tural Ali


2 Answers

Assigning different values to variables is fine. It's what variables are for, really. It only becomes a problem if you assign different meanings to the variables. For example, re-using a variable called $query to hold something that isn't a query can be confusing and unintuitive to anybody who has to support the code (including yourself after you've forgotten about it).

As long as the variables maintain what they mean in the context of the code, you can re-assign them all you want.

like image 144
David Avatar answered Oct 07 '22 14:10

David


It depends, and there is no single answer, you know.

In cases you mention ($query, $row) it's better to reuse variabls.

So, just make it sensible and don't make yourself a trouble out of nowhere.

like image 20
Your Common Sense Avatar answered Oct 07 '22 14:10

Your Common Sense