[Previous] [Contents] [Next]


Types


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

In this book, and particularly in this chapter, 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';

[Previous] [Contents] [Next]