Variables in PHP are identified by a dollar sign followed by the variable name. Variables don't need to be declared, and they have no type until they are assigned a value. The following code fragment shows a variable $var assigned the value of an expression, the integer 15. Therefore, $var is defined as being of type integer.

$var = 15;

Because the variable in this example is used by assigning a value to it, it's implicitly declared. Variables in PHP are simple: when they are used, the type is implicitly defined-or redefined-and the variable implicitly declared.

The variable type can change over the lifetime of the variable. Consider an example:

$var = 15;
$var = "Sarah the Cat";

This fragment is acceptable in PHP. The type of $var changes from integer to string as the variable is reassigned. Letting PHP change the type of a variable as the context changes is very flexible and a little dangerous.

Variable names are case-sensitive in PHP, so $Variable, $variable, $VAriable, and $VARIABLE are all different variables.

One of the most common sources of bugs in PHP is failing to detect that more than one variable has accidentally been created. The flexibility of PHP is a great feature but is also dangerous. We discuss later how to set the error reporting of PHP so that it creates warning messages sensitive to unassigned variables being used.


PHP has four scalar types-boolean, float, integer, and string-and two compound types, array and object.

We present function prototypes that specify the types of arguments and return values. There are many functions that allow arguments or return values to be of different types, which we describe as mixed.

Variables of a scalar type can contain a single value at any given time. Variables of a compound type-array or object-are made up of multiple scalar values or other compound values. Arrays and objects have their own sections later in this chapter. Other aspects of variables-including global variables and scope-are discussed later, with user-defined functions.

Boolean variables are as simple as they get: they can be assigned either true or false. Here are two example assignments of a Boolean variable:

$variable = false;
$test = true;

An integer is a whole number, while a float is a number that has an exponent and a fractional part. The number 123.01 is a float, and so is 123.0. The number 123 is an integer. Consider the following two examples:

// This is an integer
$var1 = 6;
// This is a float
$var2 = 6.0;

A float can also be represented using an exponential notation:

// This is a float that equals 1120
$var3 = 1.12e3;
// This is also a float that equals 0.02
$var4 = 2e-2

You've already seen examples of strings earlier, when echo( ) and print( ) were introduced, and string literals are covered further in Section 2.6. Consider two example string variables:

$variable = "This is a string";
$test = 'This is also a string';


Constants associate a name with a simple, scalar value. For example, the Boolean values true and false are constants associated with the values 1 and 0, respectively. It's also common to declare constants in a script. Consider this example constant declaration:

define("pi", 3.14159);
// This outputs 3.14159
echo pi;

Constants aren't preceded by a $ character; they can't be changed once they have been defined; they can be accessed anywhere in a script, regardless of where they are declared; and they can only be simple, scalar values.

Constants are useful because they allow parameters internal to the script to be grouped. When one parameter changes-for example, if you define a new maximum number of lines per web page-you can alter this constant parameter in only one place and not throughout the code.