498 Creating Comments ?> The tags define the beginning and ending of a PHP program. PHP programs (scripts) are usually stored in a file that ends with the .php file extension. Creating Comments To write a comment in PHP, you have three choices: //, #, or /* and */ symbols. Both the double slash (//) and the number (#) characters are used to create comments on a single line, such as // This is the beginning of the PHP program. # This is the end of the PHP program If you want to write a comment over multiple lines, use the /* and the */ characters, such as /* This is the beginning of the PHP program. If a comment extends over multiple lines, It’s easier to use these types of comment symbols instead. */
Declaring Variables 499 Declaring Variables PHP variables can hold any type of data, so a variable might hold a string one moment and a number the next. To declare a variable in PHP, you must begin every variable name with the dollar symbol ($), such as Book V Chapter 4 $VariableName = value; PHP VariableName can be any descriptive name, but PHP is a case-sensitive language so $MyAge is considered a completely different variable from $myage. Some programmers use uppercase letters to make variable names easier to find, and others use all lowercase. One unique feature of PHP is its ability to reference the same value. For example, consider the following code: $myage = 35; $yourage = $myage; $myage = 109; In this example, the $myage variable is initially set to 35 and the $yourage variable is set to the $myage variable, which means the $yourage variable also contains the value of 35. The third line stores the value 109 into the $myage variable, but the $yourage variable still holds the value of 35. By referencing variables with the ampersand symbol (&), PHP allows a variable to contain identical data without specifically assigning those values. For example: $myage = 35; $yourage = &$myage; $myage = 109; The second line in the preceding PHP code tells the computer that the $yourage variable references the $myage variable, so whatever value the $myage variable contains from now on will automatically get stored in the $yourage variable. After the third line, the $myage variable now contains the value of 109, so the $yourage variable contains 109 too.