Today's articles

Exercise 3 of 6 in chapter 1 · 100 XP · 4 objectives checked by tests

Topics covered: ArrayAssociative arrayforeach

Madame Lou has gathered today's news. Gaston, the village constable, wrote two articles, and she wrote one herself. A page per story, by hand? No way: PHP knows how to repeat itself.

Your mission

  1. The articles are stored in the $articles array. Add a fourth article to it, written on the same model as the other three: its titre (title) is 'The soup contest is open' and its auteur (author) is 'Odette'.
  2. Inside <main class="articles">, go through the array with a foreach loop.
  3. For each article, print an <article class="article"> containing its title in an <h2> and its author in <p class="auteur">By Madame Lou</p>.

Reminder

An array stores several values in a single variable. An associative array stores them under keys: $article['titre']. Here, $articles is an array of arrays, and foreach visits each element, one by one, from first to last.

To add an element at the end of an existing array, there is also a short form: $articles[] = ['titre' => '…', 'auteur' => '…'];. The empty brackets mean “next in line”. You'll meet it again later, to fill an array inside a loop.

Notice that the article about the mayor's cat has its title in double quotes: "The mayor's cat is missing". That's because of the apostrophe: inside single quotes, it would end the string too early.