In the previous chapter, we introduced you to some of the more basic data types in PHP, notably numbers, Booleans, and text strings. We will now round out our introduction to data types in PHP and briefly show you arrays, objects, and a few other special types.
Arrays are a powerful way to group data (not even necessarily of the same type) with a number of flexible ways to access them. They can be used as simple numbered arrays, with which programmers are familiar, or they can be more flexible ordered maps, in which values are accessed via keys. The latter type is often referred to as associative arrays.
To declare an array, use the array method. This takes an initial set of values and returns an array object holding them all.
<?php $fruit = array("apple", "orange", "cherry", "banana"); $ten_primes = array(1, 2, 3, 5, 7, 11, 13, 17, 19, 23); $mixed_up = array(234.22, "oink oink", 343, array(3, 4, 5), TRUE); ?>
By default, values within an array are assigned an integer name or key (also referred to as an index in this case) that starts at 0. To add a new element to the array, the following syntax is used:
<?php $fruit = "papaya"; // element at index 4 newly added. $fruit = "guava"; // element at index 5, etc ... ?>
You can also specify the index of the new item to add. If it is greater than the last index in the array, there is a 'gap' in the numbering, which is normal.
<?php $fruit = "nespola"; ?>
You can access elements on the array by simply providing the index inside square brackets:
<?php echo $fruits; // prints: banana ?>
However, as mentioned before, you can also specify keys with a string value instead of the default number assigned to it (which is also just a key). To do this, indicate a key-value pair with the => operator when creating the array.
<?php $myFavourite = array("car" => "Ferrari", "number" => 21, "city" => "Ouagadougou", "band" => "P.J Harvey"); echo $myFavourite["number"]; // prints: 21 ?>
Arrays are a remarkably powerful language construct in PHP, which we will cover in more detail in Chapter 5, "Working with Arrays."
While versions prior to PHP 5 supported object-oriented programming, PHP saw a major reworking for the latest version, and it is something you will encounter more and more as you work with the language. In short, object-oriented programming is a way to implement new data types (often called "objects" or "classes") in which the data and implementation are closely coupled. Thus, instead of having to implement a series of functions, you can implement methods and variables on the data directly.
To access variables or methods on an object, you use the -> operator in PHP. If we were to have a class representing a circle with the location of the center (x, y) and its radius as member variables and a method to compute its area, we might write code as follows:
<?php // we'll learn about the "new" operator in Chapter 4 $circle = new Circle(); $circle->x = 20; $circle->y = 20; $circle->radius = 5.4; echo "The area of the circle is: " . $circle->computeArea(); ?>
The objects mentioned here are another extremely powerful language construct that will be covered in greater detail in Chapter 4, "Object-Oriented Programming."
Special Types and Values
We now turn our attention to a few special types and values that occur in PHP that do not fit into any of the categories described earlier.
NULL is a special type and value in PHP that indicates "no value." Variables can be NULL if:
NULL is very different from the integer value 0 and empty string '' because the latter two are set values. You can test to see if a value is NULL by calling the is_null method.
<?php $myvar = nULl; // NULL is case insensitive echo is_null($myvar); // prints "1." ?>
There are times when PHP needs to hold on to an object that does not necessarily come from PHP, such as a database connection or a handle to an operating system object. These are special variables called resources.
Resources are typically passed between functions that know how to work with them. They are automatically freed by the PHP language engine when they are no longer needed. Most of the time, you will not even realize you are working with resources and will not need to worry about them. If you use the var_dump method on a resource variable, you will see something similar to
resource(2) of type (mysql link)