PHP
Adding tasks
Create a tasks array and write a function that adds a new task.
Wrapping task addition in a function
In the first lesson you added a single task with $tasks[]. Instead of writing the same code again, we wrap it in a function. The addTask function takes the tasks array and the new task's title as parameters and returns the updated array.
In PHP, arrays are passed by value; the original array is not changed inside the function. So the function works on a copy, appends the new task, and gives it back with return. At the call you write $tasks = addTask($tasks, ...) to update the real variable.
This way you can add as many tasks as you like with the same function; the rest of the code stays the same.
The body of the addTask function
The function signature is function addTask(array $tasks, string $title): array. Here array $tasks is the tasks array and string $title is the title to add; the trailing : array is the return type.
Inside the body you append the new task as an associative array with $tasks[] = ["title" => $title, "done" => false]. done starts as false because the new task is not finished yet. Finally return $tasks; gives back the updated array.
Exercise Write the addTask function and add two more tasks
Define the addTask function with the signature function addTask(array $tasks, string $title): array. In the body append the new task with $tasks[] = ["title" => $title, "done" => false] and give the array back with return $tasks;. Then call $tasks = addTask($tasks, "Buy milk") and $tasks = addTask($tasks, "Take out trash"); after that add the line echo "Task count: " . count($tasks) . "\n";. The second output line will then read Task count: 3.
Show example solution
function addTask(array $tasks, string $title): array. Body: $tasks[] = ["title" => $title, "done" => false]; return $tasks;. Two calls: $tasks = addTask($tasks, "Buy milk"); and $tasks = addTask($tasks, "Take out trash");. Then echo the count.
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";
Test yourself
-
What should the addTask function return?
- Nothing (void)
- A string
- The updated tasks array
- The task count (int)
Show answer
The updated tasks array — Correct. The function returns the array with the new task appended, so $tasks = addTask(...) updates the variable.
-
Complete the return type
function addTask(array $tasks, string $title): ____ {
return $tasks;
}
Show answer
function addTask(array $tasks, string $title): array {
return $tasks;
}
-
Match the function parts
- function addTask
- declares the function
- array $tasks
- array parameter
- string $title
- string parameter
- : array
- return type
Next lesson: Listing tasks