Variable Syntax

I l @ ve RuBoard

In PHP, all variables begin with a dollar sign ($), followed by the variable name itself. This name must begin with either a letter (A “Z, a “z) or the underscore (_), followed by any number of letters , underscores, or numbers , used in combination or not. You may not use spaces within the name of a variable. Instead, the underscore is commonly used to separate words in a variable name.

Script 2.1. It is always better to include too many comments than too few because what seems obvious at the time of initial programming may not be so understandable when you go back in months later.

graphics/02sc01.jpg

Keep in mind that variables are case-sensitive. Consequently, "$variable" and "$Variable" are two different constructs, although it would never make sense to use two variables with such similar names. One should quickly get into the habit of creating variable names that make sense on their own, as well as using comments to indicate the purpose of variables (Script 2.1). These habits will reduce errors and make revisiting your work less taxing. For example, "$FirstName" is more useful than "$FN" and putting in a comment that details what a variable's purpose is will make your work abundantly clear. In fact, you may decide that "$first_name" is a better variable name than "$FirstName" because there are no capital letters to get right and the words are separated for clarity. No matter how you decide to name your variables, the most important thing to remember is that whatever convention you use, be consistent. This will help you avoid making trivial errors in your programming.

Unlike some other languages, in PHP you neither have to declare what a variable is (to declare a variable is to assign it a type ”I'll cover variable types in Types of Variables ) nor initialize it prior to first use (to initialize a variable is to create it). With PHP, a variable exists and is defined the first time you use it.

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net