Kristina Anderson wrote:
One thing that I've taken away from this (aside from feeling like an
idiot!) is that one reason for my introduction of these bugs is a
holdover from my "ASP" coding style where I name each query $query1,
query2, etc rather than what I see in a lot of PHP code I've inherited,
where each query is simply named $query, even if there are 6 or 7 of
them on the page.
What is the accepted best practice here? I find it very confusing to
try to read code with 16 items each named $query or $result, but
my "ASP" style code is clearly introducing other editing issues.
I don't know if it is "best practice", but I give the query string variables
descriptive names. Typically I use three variables for a SELECT query named
such as
- $getcategoriesquery => that is the variable that holds the SQL query string
- $getcategoriesqueryrun => that is the variable that holds the return value
from mysql_query()
- $getcategoriesqueryresult => this is typically an array that holds the
currently read out row from which I would get the value to be added to the
$categories array
I suspect this to be wasteful as PHP holds on to the variables even when I no
longer use them within the script, but I found it to be really helpful when
using the ....queryrun as a flag to decide if some query worked or generated a
result when using it in an If block or such 50 lines further down. This
verbose naming helps me a lot to figure out if the current value stored in a
variable is plausible in regards to the variable name and the expected value.
I don't know if this is the best approach, but I am sure it is better than to
name all queries $query or just to number them. Half a year from now nobody
knows what the difference between $query5 and $query3 is unless the line where
the SQL string is assigned is quickly found.
David
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php