NYCPHP Meetup

NYPHP.org

[nycphp-talk] About Human Readable Variable Names Moving Among PHP, HTML and MySQL

David Krings ramons at gmx.net
Wed Sep 27 07:25:58 EDT 2006


Hi,

         just for the reasons that you mentioned, I never use spaces in 
variable names. I typically ue the "_" as space replacement. I also tend to 
use telling names. For example:
- I do not use $i, but $counter as counter variable
- I always craft SQL queries as a string first before using it in 
mysql_query. All my query string variables always end in "query". Similar 
for the result of mysql_query which I assign to a variable that has the 
same first portion of the name as the query, but ends in "queryrun". Any 
results if available are assigned to a variable that has the same first 
portion of the name as the query, but ends in "queryresult".
- All variables used in functions are prefixed by "fct_"
- All variables that I get from and / or write back to session are prefixed 
with "session"
- As you indicated, I try to name the form element, the variable that gets 
the value from $_POST, and the table column in the database the same

I think what is more necessary than spaces is to have the variable names 
mean something and consistency in nomenclature. And on top of that, 
document everything in an external document. Of course, comment liberally, 
especially when initializing variables (I initialize all of them at the 
start of a script) add a comment what that variable is for. Add a general 
note where the script gets values from (script name, and if from POST 
and/or SESSION).

My approach isn't perfect, but it keeps myself out of trouble. My variable 
names are David Krings-reable, I have no idea if they are humanly readable 
as so far only one other person ever used any of my scripts (MP3 script 
using the Flash MP3 player from Jeroen Wijering).

IMHO, there are better things to worry about than spaces or no spaces.

         David K.


At 06:34 AM 9/27/2006, you wrote [truncated]:
>I'm always attempting a tight level of coordinated, human readable 
>consistent naming for my data, i.e. I try to:
>a) use the same name such as "First Name" on my HTML form,
>b) use that variable name in my PHP script (i.e. $First_Name), and
>c) get the field name in tables in MySQL to match (again i.e. FirstName).
>
>My issues are about the spaces in data names. (To me, spaces is what makes 
>data names
>human readable.)
>
>In MySQL:
>I forget, you can use spaces, but should surround in tics, right. Like so: 
>'First Name'.
>
>In PHP:
>Can't have spaces.
>
>In HTML forms:
>Can have spaces, but might cause problems.
>
>So I'm just asking for an opinion, do you deal with spaces in variables?
>Never use spaces?
>
>Is the problem of spaces just an old legacy, cross platform 
>Windows/Mac/Unix bad memory I have?
>




More information about the talk mailing list