php · level 1 · unit 6 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An keeps many values together in one box.
So far each box has held one value. An array holds many values in one box, in order, like a shopping list. Each value in it is an .
Make one with square brackets and commas between the items: ["bread", "milk", "eggs"]. count tells you how many items an array holds.
echo cannot show a whole array. It stops with Array to string conversion. Instead, will glue the items into one string, with a comma and a space between each.
<?php
$shopping = ["bread", "milk", "eggs"];
echo count($shopping) . " things to buy\n";
echo implode(", ", $shopping) . "\n"; An array can hold numbers too, like [7, 9, 4], or no items at all, [].
guess first
Can echo show a whole array? Make a guess.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$snacks = ["crisps", "apple", "popcorn"];
echo $snacks;
your turn
Your turn. List three favourite things.
Type three of your favourite things in the blanks, keeping each one inside its quotes. Then run it.
fix it
Three snacks went in, but nothing came out. Can you fix it?
- It is a ParseError on line 2. Look at what sits between each pair of items.
- Every item needs a comma between it and the next one. One is missing.
- Add a comma after "apple", then press run.
nailed it
you packed lots of values into one box. :D
- An array holds many items, in order, in square brackets.
counttells you how many items there are.implodeglues the items into one string to echo.
new words in your collection
- array
- item
- implode
This lesson isn’t saved. A free account remembers every lesson you finish.