The loop has grown: banner, section, author, standfirst… And Léo, the mayor's grandson, has promised to slip a <script> tag into a title “just to see”. Time to tidy this code into a function, and to make it careful.
Your mission
- Declare a function
afficherArticle(array $article): string(“show article”), which returns the full HTML of an article (<article>tag, banner, section, title, author and standfirst) instead of printing it. - In the function, pass the title, the author and the standfirst through
htmlspecialchars()before putting them in the HTML. - In the loop, only one line is left:
echo afficherArticle($article);.
The preview must not change by a single pixel: it's the code that gets better.
The tests will feed your function a title, an author and some standfirsts from Léo, short and long, to check that they stay plain text.
Reminder
A function gives a name to a piece of code. It takes parameters, and return sends its result back to whoever called it. The types (array, string) say what it expects and what it gives back: PHP rejects a wrong argument rather than guessing. htmlspecialchars() turns < into <: the browser shows the tag instead of running it. To build the HTML bit by bit, store it in a variable and extend it with .=: $html .= '<h2>…</h2>'; adds to the end of $html, then return $html; returns it.