PHP
Project final - To-do list
Combine all parts and finalize a working to-do list.
Project final - To-do list
The app is almost done: you added tasks, listed them and completed one. The last step is to show a summary to the user. A line like "Completed: 1/3" gives the state of the list at a glance.
We'll write a function called countDone. It will walk the tasks array, count those whose done value is true, and return an int. Then we'll combine that number with the total task count (count($tasks)) to print the summary line.
countDone and the summary line
The function signature is function countDone(array $tasks): int. In the body we first reset a counter with $done = 0. Then we walk all tasks with foreach ($tasks as $task); if $task["done"] is true we increment the counter with $done++.
We capture the returned value with $done = countDone($tasks). The total task count comes from count($tasks). The summary line is written as echo "Completed: " . $done . "/" . count($tasks);.
Exercise Write the countDone function and print the summary line
Define the countDone function with the signature function countDone(array $tasks): int. In the body first write $done = 0;; then walk with foreach ($tasks as $task); if $task["done"] is true do $done++. The function ends with return $done;. After the function add $done = countDone($tasks); and echo "Completed: " . $done . "/" . count($tasks); (no \n at the end). The last output line will be Completed: 1/3.
Show example solution
function countDone(array $tasks): int. Body: $done = 0; foreach ($tasks as $task) { if ($task["done"]) { $done++; } } return $done;. Then $done = countDone($tasks); echo "Completed: " . $done . "/" . count($tasks);
Example solution:
<?php
// Initialize the tasks array and add the first task
$tasks = [];
$tasks[] = ["title" => "Buy bread", "done" => false];
echo "Task count: " . count($tasks) . "\n";
// Define the addTask function and add two more tasks
function addTask(array $tasks, string $title): array {
$tasks[] = ["title" => $title, "done" => false];
return $tasks;
}
$tasks = addTask($tasks, "Buy milk");
$tasks = addTask($tasks, "Take out trash");
echo "Task count: " . count($tasks) . "\n";
// Write and call the listTasks function
function listTasks(array $tasks): void {
foreach ($tasks as $i => $task) {
$mark = "[ ]";
if ($task["done"]) {
$mark = "[x]";
}
echo ($i + 1) . ". " . $task["title"] . " " . $mark . "\n";
}
}
listTasks($tasks);
// Write the completeTask function and complete the first task
function completeTask(array $tasks, int $index): array {
if (isset($tasks[$index])) {
$tasks[$index]["done"] = true;
}
return $tasks;
}
$tasks = completeTask($tasks, 0);
listTasks($tasks);
// Write the countDone function and print the summary line
function countDone(array $tasks): int {
$done = 0;
foreach ($tasks as $task) {
if ($task["done"]) {
$done++;
}
}
return $done;
}
$done = countDone($tasks);
echo "Completed: " . $done . "/" . count($tasks);
Project complete
The to-do list app is complete. Over five lessons you have:
- added tasks as associative arrays
- written a function that adds new tasks with
addTask - numbered and printed the list with
foreach - marked tasks as done with
completeTask - counted the completed ones and printed a summary with
countDone
This structure works the same way in real apps: task managers, shopping lists, step trackers. In the next sections you'll make the list persistent with object-oriented programming and a database.
Test yourself
-
Why do we print a summary line at the end?
- Because colored output is needed
- Because the user wants progress at a glance
- Because listTasks does not run
- Because the done key must be deleted
Show answer
Because the user wants progress at a glance — Correct. A summary like "Completed: 1/3" shows the state of the list right away.
-
Match the project parts
- addTask
- adds a task
- listTasks
- lists the tasks
- completeTask
- completes a task
- countDone
- counts done tasks
-
Reset the counter
$done ____;
Show answer
$done = 0;
Next lesson: Replacing with str_replace()