|
Chapter 4
Using Variables in PHP Scripts
In This Chapter
Naming variables
Assigning values to variables
Removing variables
Using constants
Handling errors
V
ariablesare containers that hold information. First, you give a variable a
name, and then you can store information in it. For example, you could
name a variable
$age
and store the number 21 in it. After you store informa-
tion in a variable, you can use that variable later in the script.
When using PHP on the Web, variables are often used to store the informa-
tion that users type into an HTML form, such as their names. You can then
use the variable later in the script, perhaps to personalize a Web page by dis-
playing the user’s name, as in, for example,
Welcome Sam Smith
.
In this chapter, you find out how to create variables, name them, and store
information in them. You also discover how to handle errors.
Naming Variables
Variable names or identifiersshould be very descriptive. I have seen scripts
where all the variables were named
$var1
,
$var1
,
$var2
, and so on. It may
seem straightforward to name variables like this, but two years from now
when you come back to the script, it will take forever to figure out what
information is in each variable. PHP won’t care or get confused, but humans
trying to follow the script will have a hard time. Make your scripts much
easier to understand by using descriptive variable names like
$firstName
,
$directory_name
, or
$DateOfBirth
.
|