Menu
Coddy logo textTech

Displaying All Tasks

Part of the Fundamentals section of Coddy's PHP journey — lesson 56 of 71.

challenge icon

Challenge

Easy

In the previous lesson, you added a new task to the $todos array. Now, add the ability to display all tasks in a formatted list.

Read four lines of input for task descriptions (three initial tasks plus one new task, as in the previous lesson).

After building the $todos array with all four tasks, loop through the array and print each task in the following format:

- [task description] (Pending)

Since all tasks have "completed" set to false, each task should display (Pending) after the description.

Print each task on a separate line.

Example:

If the inputs are Buy groceries, Walk the dog, Finish homework, and Call mom, the output should be:

- Buy groceries (Pending)
- Walk the dog (Pending)
- Finish homework (Pending)
- Call mom (Pending)

If the inputs are Clean room, Read book, Exercise, and Cook dinner, the output should be:

- Clean room (Pending)
- Read book (Pending)
- Exercise (Pending)
- Cook dinner (Pending)

Try it yourself

<?php
// Read three task descriptions
$task1 = trim(fgets(STDIN));
$task2 = trim(fgets(STDIN));
$task3 = trim(fgets(STDIN));

// Read the new task to add
$newTask = trim(fgets(STDIN));

// Create a $todos array containing three task arrays
// Each task should be an associative array with "task" and "completed" keys
$todos = [
    ["task" => $task1, "completed" => false],
    ["task" => $task2, "completed" => false],
    ["task" => $task3, "completed" => false]
];

// Add the new task to the end of the array


// Loop through the array and print each task in the formatted list

?>

All lessons in Fundamentals