All the programming languages that we teach use variables and although some things differ, they're common in that they provide a way to name a memory location for use later on. And they're common (in the languages that we teach) in that a variable name is almost always ...
followed by letters, digits and underscores.
In the case of PHP, variable names are preceded by a $; in the case of Perl, they're preceded by a $ a @ a % an & or a *. In the case of Tcl, they're preceded by a $ when being used but not when being set.
In all the languages that we teach, a 52 letter alphabet is used - in other words, variable names are case sensitive.
Why do we care so much about variable names to write a complete article about them? Because a good choice of variable names can turn an unreadable and un-maintainable piece of code into one that's well documented and easy to debug. Commenting your program is very important, and using descriptive variable names in many ways is just as significant.
As you set your own standards (please use my document here as a guideline) think ...
Will my variable names by understood
Will they provide information to the maintenance programmer
Will they be easy to get right as more code is written
Will they get confused with one another
CASE AND UNDERSCORING
Yes, I know you can mix upper and lower case - but do you really want to? It's so easy to use upper case for one variable, lower case for another, then to forget which you used later on.
Suggestion - keep all your "simple" variables to lower case throughout. Depending on the language you're using, you may wish to capitalise object or class names, or package names. And you may wish to write constants all in capitals.
Should you use underscores in variable names? It's up to you - if your variable name is several words joined together, you may wish (consistently) to place an underscore between words and / or to capitalise the first letter of each embedded word - sometimes known as "camel case".
In Python, you're recommended to start a private variable name with a single underscore, and an intensely private name with a double underscore. System names start AND end with double underscore.
It's so easy when you learn to program to use variable names like "i" and "j" but this will make your longer pieces of code almost impossible to maintain later if you continue to do so throughout. I've heard this described as "job protection" coding.
A second convention is to use abbreviations for variables - all programmers are lazy when it comes to typing, so perhaps you would prefer to write (as I once did in an example) $n/f rather that $number_pieces_fruit. But guess which of those is easier to understand later!
I'm going to recommend that you use longer variable names with full words in them rather than shortenings ... typically, I would expect to see variable names of 6 to 16 characters in length.
BUT ... that's a lot of typing!
a) There's nothing wrong with consistently using a limited number of common shortenings - "no" for "number" for example - so $no_pieces_fruit, and $info is acceptable (or perhaps preferable!) in place of $information
b) For tight code - variables used over just a few lines, such as a loop counter, you may go with variables as short as single letters - almost algebraic names such as $i and $j, or $r and $c (for row and column) or $x and $y (the two axes of a graph).
ENGLISH OR AMERICAN
center or centre? color or colour? capitalize or capitalise?
Your choice but - please - set down an organisation standard that says that "we spell the English way" or "we spell the American way".
See also Perl Standards
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via our main web site