1.6. Using PHP Variables

Variables are containers used to hold information. A variable has a name, and information is stored in the variable. For instance, you might name a variable $age and store the number 12 in it. After information is stored in a variable, it can be used later in the script. One of the most common uses for variables is to hold the information that a user types into a form.

1.6.1. Naming a variable

When you're naming a variable, keep the following rules in mind:

  • All variable names have a dollar sign ($) in front of them. This tells PHP that it is a variable name.

  • Variable names can be any length.

  • Variable names can include letters, numbers, and underscores only.

  • Variable names must begin with a letter or an underscore. They cannot begin with a number.

  • Uppercase and lowercase letters are not the same. For example, $firstname and $Firstname are not the same variable. If you store information in $firstname, for example, you can't access that information by using the variable name $firstName.

When you name variables, use names that make it clear what information is in the variable. Using variable names like $var1, $var2, $A, or $B doesn't contribute to the clarity of the script. Although PHP doesn't care what you name the variable and won't get mixed up, people trying to follow the script will have a hard time keeping track of which variable holds what information. ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.