Home > Articles

  • Print
  • + Share This
This chapter is from the book

Understanding Identifiers

Identifiers are the names of variables. (The names of functions and classes are also identifiers; we look at functions and classes in Chapter 5, “Reusing Code and Writing Functions,” and Chapter 6, “Object-Oriented PHP.”) You need to be aware of the simple rules defining valid identifiers:

  • Identifiers can be of any length and can consist of letters, numbers, and underscores.

  • Identifiers cannot begin with a digit.

  • In PHP, identifiers are case sensitive. $tireqty is not the same as $TireQty. Trying to use them interchangeably is a common programming error. Function names are an exception to this rule: Their names can be used in any case.

  • A variable can have the same name as a function. This usage is confusing, however, and should be avoided. Also, you cannot create a function with the same name as another function.

You can declare and use your own variables in addition to the variables you are passed from the HTML form.

One of the features of PHP is that it does not require you to declare variables before using them. A variable is created when you first assign a value to it. See the next section for details.

You assign values to variables using the assignment operator (=) as you did when copying one variable’s value to another. On Bob’s site, you want to work out the total number of items ordered and the total amount payable. You can create two variables to store these numbers. To begin with, you need to initialize each of these variables to zero by adding these lines to the bottom of your PHP script.

$totalqty = 0;
$totalamount = 0.00;

Each of these two lines creates a variable and assigns a literal value to it. You can also assign variable values to variables, as shown in this example:

$totalqty = 0;
$totalamount = $totalqty;
  • + Share This
  • 🔖 Save To Your Account