Introduction
A type specifies the amount of memory that allocates to a value associated with it. A type also determines the operations that you can perform on it.
PHP has ten primitive types including four scala types, four compound types, and two special types:
Scalar types
A variable is a scalar when it holds a single value of the type integer, float, string, or boolean.
Integer
Integers are whole numbers defined in the set {…-3,-2-,-1,0,1,2,3…}. The size of the integer depends on the platform where PHP runs.
The constant PHP_INT_SIZE
specifies the size of the integer on a specific platform. PHP uses the int keyword to denote the integer type.
Ex: Integer literals
1 2 3 4 5 6 7 8 | <?php $a = 1234; // decimal number $a = 0123; // octal number (equivalent to 83 decimal) $a = 0o123; // octal number (as of PHP 8.1.0) $a = 0x1A; // hexadecimal number (equivalent to 26 decimal) $a = 0b11111111; // binary number (equivalent to 255 decimal) $a = 1_234_567; // decimal number (as of PHP 7.4.0) ?> |
Float
Floats are floating-point numbers, which are also known as floats, doubles, or real numbers.
PHP uses the IEEE 754 double format to represent floats. Like other programming languages, floats have limited precision.
PHP uses the float
keyword to represent the floating-point numbers. The following example illustrates the floating-point numbers in PHP:
1 2 3 4 | <?php $price = 10.25; $tax = 0.08; |
Boolean
Boolean represents a truth value that can be either true
or false
. PHP uses the bool
keyword to represent the Boolean type.
The bool type has two values true
and false
. Since keywords are case-insensitive, you can use true
, True
, TRUE
, false
, False
, and False
to indicate boolean values.
Ex:
1 2 3 4 | <?php $is_admin = true; $is_user_logged_in = false; |
PHP treats the following values as false
:
- The
false
keyword. - The integer 0 and -0 (zero).
- The floats 0.0 and -0.0 (zero).
- The empty string (
""
,''
) and the string “0”. - The empty array (
array()
or[]
). - The
null
. - The
SimpleXML
objects created from attributeless empty elements.
The values that are not one of these falsy values above are true
.
String
A string is a sequence of characters surrounded by single quotes (‘) or double quotes (“).
1 2 3 4 | <?php $str = 'PHP scalar type'; $message = "PHP data types"; |
Compound types
Compound data includes the values that contain more than one value. PHP has two compound types including array and object.
Array
An array is an ordered map that associates keys with values. For example, you can define a list of items in a shopping cart like this:
1 2 3 | <?php $carts = [ 'laptop', 'mouse', 'keyboard' ]; |
The $carts
array contains three string values. It maps the index 0
, 1
, and 2
to the values 'laptop'
, 'mouse'
, and 'keyboard'
. The $carts
is called an indexed array because it uses numeric indexes as keys.
To access a value in an array, you use the square brackets:
1 2 3 4 5 | <?php echo $carts[0]; // 'laptop' echo $carts[1]; // 'mouse' echo $carts[2]; // 'keyboard' |
Besides numeric indexes, you can use strings as keys for the array elements. These arrays are known as associative arrays.
Ex:
1 2 3 4 5 6 7 | <?php $prices = [ 'laptop' => 1000, 'mouse' => 50, 'keyboard' => 120 ]; |
To access an element in an associative array, you specify the key in the square brackets.
Ex:
1 2 3 4 5 | <?php echo $prices['laptop']; // 1000 echo $prices['mouse']; // 50 echo $prices['keyboard']; // 120 |
Object
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Let’s assume we have a class named Car
that can have properties like model, color, etc. We can define variables like $model
, $color
, and so on, to hold the values of these properties.
When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
If you create a __construct()
function, PHP will automatically call this function when you create an object from a class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Car { public $color; public $model; public function __construct($color, $model) { $this->color = $color; $this->model = $model; } public function message() { return "My car is a " . $this->color . " " . $this->model . "!"; } } $myCar = new Car("red", "Volvo"); var_dump($myCar); |
Special types
PHP has two special types: null and resource
Null
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
Variables can also be emptied by setting the value to NULL:
1 2 3 | $x = "Hello world!"; $x = null; var_dump($x); //NULL |
Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
A common example of using the resource data type is a database call.
We will not talk about the resource type here, since it is an advanced topic.