null, Arrays, Objects and Resources

3.null, Arrays, Objects and Resources

M

In this chapter

We'll meet the rest of PHP's value types: null, which means "no value"; arrays, which hold many values; objects, which bundle data and behaviour; and resources, PHP's handles to things outside it, like open files — with a first look at each, before later Acts go deep.

12–14 min

The Problem in Real Life

Mia runs var_dump() on one whole booking from the partner site, to see exactly what arrives. The answer is much longer than she expected.

Inside, there's a NULL where the customer's note should be. A list of time slots in square brackets, marked array(2). The customer is shown as object(stdClass)#1 with fields of its own. And at the bottom, the connection to the partner's file: resource(2) of type (stream).

"Four new types," says Marcus. "You won't master them today — arrays and objects get whole Acts later. But you need to recognise every one of them when PHP shows it to you."

M

What are object, array and resource — and why is NULL in capital letters?

Mia

Single Values vs. Everything Else

A value that means nothing

null shows up in many places — but what does "no value" really mean?

Many values in one

A booking has a customer, slots and a price. Single values can't hold all that.

Things outside PHP

Open files and connections aren't values like numbers — PHP needs a way to point at them.

null, Arrays, Objects and Resources

Chapter 2 covered the four scalar types — single, simple values. PHP has four more kinds of value, which you'll see in var_dump() output from now on:

  • null: "no value".
  • array: many values in one, as a list or with named keys.
  • object: a bundle of data (and, later, behaviour) built from a class.
  • resource: a handle to something outside PHP, like an open file.

null: null is a type with exactly one value: null itself. It means "no value here". You've met it already: reading a variable that was never set gives null (Act 03, chapter 1), and isset() treats null as not set (Act 03, chapter 2). You'll also get null from a function that returns nothing, and you'll often use it on purpose, for a value that's optional — like a customer's note that wasn't written. var_dump() prints it in capitals, NULL, but in your code write null in lower case. The ?? operator from Act 03 is the everyday way to replace it with a default.

null: the one value meaning "no value"
<?php
$note = null; // no note was written
var_dump($note); // NULL
echo $note ?? "No note"; // No note

array: an array holds many values in one variable. It can be a simple list, where PHP numbers the items from 0 — ["9:00", "9:30"] — or a collection with named keys, where you choose the name of each item — ["haircut" => 25, "beardTrim" => 10]. You read one item with square brackets: $slots[0] or $prices["haircut"]. In var_dump(), an array shows array(2) (it has 2 items) and then each key and value, as below. Arrays are PHP's most-used structure; Act 08 is all about them.

A list and a collection with named keys
<?php
$slots = ["9:00", "9:30"]; // a list: keys 0 and 1
$prices = ["haircut" => 25, "beardTrim" => 10]; // named keys
echo $slots[0], "\n"; // 9:00
echo $prices["haircut"], "\n"; // 25
var_dump($prices);
What var_dump() shows for $prices
array(2) {
["haircut"]=>
int(25)
["beardTrim"]=>
int(10)
}

object: an object is a bundle of named values, called properties, that belong together — a customer with a name and a phone number, a booking with a slot and a price. Objects are built from a class, which is a blueprint that says what properties (and actions, called methods) every object of that kind has. You'll write your own classes in Act 11. PHP also has a ready-made, empty class called stdClass for simple bundles of data, used in the code below. In var_dump() an object shows its class name and a number, like object(stdClass)#1 — the number just tells objects apart.

A simple object with stdClass
<?php
$customer = new stdClass(); // new: build an object from a class
$customer->name = "Zoë"; // -> reads or sets a property
$customer->phone = "07700 900123";
echo $customer->name, "\n"; // Zoë
var_dump($customer);

var_dump() shows object(stdClass)#1 (2), then each property with its type and value.

resource: a resource is PHP's handle — a kind of ticket — for something that lives outside PHP: an open file, a network connection, a stream of data. You don't create resources directly; functions like fopen() ("open a file") give you one, and you hand it to other functions that work with it. When you close it, the same variable shows resource (closed). Resources are an old part of PHP: in modern versions, many of them have been replaced by proper objects. You'll use them with files in Act 15.

A resource: a handle to something outside PHP
<?php
$stream = fopen("php://memory", "r"); // open a stream (here: one in memory)
var_dump($stream); // resource(2) of type (stream)
fclose($stream); // close it
echo get_debug_type($stream); // resource (closed)

get_debug_type() names a value's type — you'll meet it in chapter 8.

All of PHP's value types: that makes eight types a value can have: four scalar (bool, int, float, string), two compound types that hold other values (array and object), and two special ones (null and resource). The table below lists all eight, with what var_dump() shows for each. The next chapters cover types that are only used in declarations — words like void or mixed that describe what a function accepts or returns, rather than values you can hold.

Table — All eight value types
TypeKindExamplevar_dump shows
boolScalartruebool(true)
intScalar25int(25)
floatScalar12.5float(12.5)
stringScalar"Tom"string(3) "Tom"
arrayCompound["9:00", "9:30"]array(2) { … }
objectCompoundnew stdClass()object(stdClass)#1 (0) { … }
nullSpecialnullNULL
resourceSpecialfopen(…)resource(2) of type (stream)

Key Takeaway

Besides the four scalars, PHP values can be null (the one value meaning "no value"), arrays (lists or named collections of values), objects (bundles of properties built from a class; stdClass is the ready-made one) and resources (handles to outside things like open files). var_dump() shows each type clearly.

Why This Matters

Real data is never just single numbers and words. A booking at BookBay is a bundle (an object or an array), with optional parts (null) and connections to files and databases (resources and their modern objects). Being able to read all eight types in a var_dump() means Mia can look inside any value she's handed.

And null deserves special respect: "no value" turns up everywhere, and handling it deliberately — not by accident — is what the next two chapters and their type declarations are about.

Mia can now read the whole dump. Next, Marcus asks her to add type declarations to every function in the pricing file. Most are easy — int in, int out. But one function prints a log line and returns nothing at all. One takes another function as its input. And one never returns, because it always stops the file. What types do those get? That's the next chapter.