Real World PHP

About   |   EN   |   ES

006f Data Types: Misc

The following are some more data types that we'll go over briefly now and in more depth in future chapters.

Arrays

If you think of a variable as a box that can hold a value, an array is like a series of variables linked together:

Five empty squares in a row.

The size of the array can vary, so it can have fewer or many more than the example image above. The array data type is useful when you have a group of values that are related. Each item in an array can be of any data type. For example, you could hold a list of names. Or you may have a list of addresses, or a series of numbers, or any mix of data types.

An array can be defined in one of two ways:

<?php
// Create an array using the array() method
$a = array();

// Create an array using the short array syntax (preferred)
$a = [];

The array() method is the original way that PHP used to create arrays. However, since the short array syntax was introduced, it quickly became the preferred way for programmers to create arrays. Either will work the same.

Each item in the array is accessed via an index or key:

An array of five squares, each of which has a "key" label beneath it, from 0 to 4.

PHP is a zero-based indexing language, so by default, the first item in the array has the index 0. We use square brackets to access each item.

<?php
// also valid: $a = array("a", "b" ... );
$a = ["a", "b", "c", "d"];

// Access the third item in the array:
var_dump($a[2]); // "c"

An array can hold any data type:

Five squares representing an array. Numbered array keys below each item. And each item containing a value: "A", 100, "cat", null, and 45.

Although it is normal to have each item in the array be related, it does not have to be. Additionally, the keys of the array can be set as well when creating the array, and then accessed by that key name.

<?php
// Set your own key names:
// ["key" => "value"]
$a = [
  "name" => "John Doe",
  "phone" => "555-5555",
  "age" => 97,
  "address" = NULL
];

var_dump($a['phone']); // "555-5555"

Note how the array above has set its own key names. The values are separated from the keys by a double arrow: =>.

There is a lot more to know about arrays. We will go into more details in a future chapter.

Objects

An object is a particular data type that can have both values and methods associated with it. Some objects are built-in to PHP and you can use them to do many things like interface with an external database (i.e. MySQL) or interpret an XML document. You can also create your own objects, and in that sense, you can also think of objects as custom data types that you make to fit the needs of your program.

Objects are defined using the keyword class. As such, the term "class" is used to refer to the definition and the term "object" is used to refer to an instance of that definition. You can think of it as an object being like a house and a class being like the blueprint of the house.

PHP has a basic object you can create:

<?php
$house = new stdClass();

When you create an object, you will need to use the new keyword. As a result, PHP stores objects in memory differently than it does other primitive data types. This can have an impact on speed, but in general, that rarely makes a big enough of a difference that it should convince you not to use objects.

It is possible to cast to an object, however there are not many uses of this and if you want to do it, it is important to understand the particulars. You can read more in the official documentation on converting to an object.

There is much, much more to learn about objects and object-oriented programming. We will discuss more in a future chapter.

Resources

A resource is a connection to an external resource. It could be something like a database connection, an opened file, or something similar. There are many types of resources, you can see a list here: List of Resource Types.

We will discuss several of them in detail within separate chapters.

Callbacks

A callback is a function that is passed to another function as a parameter. We will cover more in future chapters, but the idea that you need to understand is that a passed function is a data type.


Resources


Challenges

Array Investigation

There are a lot of array related functions in PHP. You can see a list of them in the PHP Documentation on Arrays. Go to that page and take a look at what they are. Try some out.

One that isn't on that list is print_r(). It is useful for displaying an array. Try it out as well.