Part 2 · Types / Typed arrays
Lesson 4 of 8

Typed arrays

Use array<int> for a list of integers. Use array<string, int> for a map from string keys to integer values, such as names and scores. The types tell you what you can add and what you get back when reading an item:

ids.ppphp
array<int> $ids = [1, 2, 3];
array<string, int> $scores = ['Maya' => 92];

The last line in the editor appends a string to the array<int> list. Press Check and read the diagnostic, then append an int instead:

ids.ppphp
$ids[] = 4;

Check again and the file passes.