- Before You Begin: Accessing PHP
- Creating a Sample Application: Bob's Auto Parts
- Embedding PHP in HTML
- Adding Dynamic Content
- Accessing Form Variables
- Understanding Identifiers
- Examining Variable Types
- Declaring and Using Constants
- Understanding Variable Scope
- Using Operators
- Working Out the Form Totals
- Understanding Precedence and Associativity
- Using Variable Handling Functions
- Making Decisions with Conditionals
- Repeating Actions Through Iteration
- Breaking Out of a Control Structure or Script
- Employing Alternative Control Structure Syntax
- Using declare
- Next
Examining Variable Types
A variable’s type refers to the kind of data stored in it. PHP provides a set of data types. Different data can be stored in different data types.
PHP’s Data Types
PHP supports the following basic data types:
Integer—Used for whole numbers
Float (also called double)—Used for real numbers
String—Used for strings of characters
Boolean—Used for true or false values
Array—Used to store multiple data items (see Chapter 3, “Using Arrays”)
Object—Used for storing instances of classes (see Chapter 6)
Three special types are also available: NULL, resource, and callable.
Variables that have not been given a value, have been unset, or have been given the specific value NULL are of type NULL.
Certain built-in functions (such as database functions) return variables that have the type resource. They represent external resources (such as database connections). You will almost certainly not directly manipulate a resource variable, but frequently they are returned by functions and must be passed as parameters to other functions.
Callables are essentially functions that are passed to other functions.
Type Strength
PHP is called a weakly typed or dynamically typed language. In most programming languages, variables can hold only one type of data, and that type must be declared before the variable can be used, as in C. In PHP, the type of a variable is determined by the value assigned to it.
For example, when you created $totalqty and $totalamount, their initial types were determined as follows:
$totalqty = 0;
$totalamount = 0.00;
Because you assigned 0, an integer, to $totalqty, this is now an integer type variable. Similarly, $totalamount is now of type float.
Strangely enough, you could now add a line to your script as follows:
$totalamount = 'Hello';
The variable $totalamount would then be of type string. PHP changes the variable type according to what is stored in it at any given time.
This ability to change types transparently on the fly can be extremely useful. Remember PHP “automagically” knows what data type you put into your variable. It returns the data with the same data type when you retrieve it from the variable.
Type Casting
You can pretend that a variable or value is of a different type by using a type cast. This feature works identically to the way it works in C. You simply put the temporary type in parentheses in front of the variable you want to cast.
For example, you could have declared the two variables from the preceding section using a cast:
$totalqty = 0;
$totalamount = (float)$totalqty;
The second line means “Take the value stored in $totalqty, interpret it as a float, and store it in $totalamount.” The $totalamount variable will be of type float. The cast variable does not change types, so $totalqty remains of type integer.
You can also use built-in functions to test and set type, which you will learn about later in this chapter.
Variable Variables
PHP provides one other type of variable: the variable variable. Variable variables enable you to change the name of a variable dynamically.
As you can see, PHP allows a lot of freedom in this area. All languages enable you to change the value of a variable, but not many allow you to change the variable’s type, and even fewer allow you to change the variable’s name.
A variable variable works by using the value of one variable as the name of another. For example, you could set
$varname = 'tireqty';
You can then use $$varname in place of $tireqty. For example, you can set the value of $tireqty as follows:
$$varname = 5;
This is equivalent to
$tireqty = 5;
This approach might seem somewhat obscure, but we’ll revisit its use later. Instead of having to list and use each form variable separately, you can use a loop and variable variable to process them all automatically. You can find an example illustrating this in the section on for loops later in this chapter.