Handling Empty List Scenarios
Part of the Fundamentals section of Coddy's PHP journey — lesson 60 of 71.
Challenge
EasyIn the previous lesson, you filtered tasks by completion status. Now, add handling for scenarios where the filtered result is empty.
Read four lines of input for task descriptions (three initial tasks plus one new task). Then read a fifth input: an integer representing the index of the task to mark as complete. Read a sixth input: an integer representing the index of the task to remove. Finally, read a seventh input: a string that is either completed or pending to filter the tasks.
After building the $todos array, marking the specified task as complete, and removing the task at the given index, filter the remaining tasks based on the filter input.
Before displaying the filtered tasks, check if any tasks match the filter:
- If no tasks match the filter, print:
No [filter] tasks found.(where[filter]is eithercompletedorpending) - If tasks match the filter, display them using the same format as before
Output format for matching tasks:
- If the task is completed:
- [task description] (Done) - If the task is pending:
- [task description] (Pending)
Example 1:
If the inputs are Buy groceries, Walk the dog, Finish homework, Call mom, 1, 1, and completed, the output should be:
No completed tasks found.Example 2:
If the inputs are Buy groceries, Walk the dog, Finish homework, Call mom, 0, 2, and completed, the output should be:
- Buy groceries (Done)Example 3:
If the inputs are Buy groceries, Walk the dog, Finish homework, Call mom, 0, 1, and pending, the output should be:
- Finish homework (Pending)
- Call mom (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));
// Read the index of the task to mark as complete
$completeIndex = (int)trim(fgets(STDIN));
// Read the index of the task to remove
$removeIndex = (int)trim(fgets(STDIN));
// Read the filter type (completed or pending)
$filter = 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
$todos[] = ["task" => $newTask, "completed" => false];
// Mark the task at the specified index as complete
$todos[$completeIndex]["completed"] = true;
// Remove the task at the specified removal index
array_splice($todos, $removeIndex, 1);
// Loop through the array and print each task that matches the filter
foreach ($todos as $todo) {
if ($filter === "completed" && $todo["completed"]) {
echo "- " . $todo["task"] . " (Done)\n";
} elseif ($filter === "pending" && !$todo["completed"]) {
echo "- " . $todo["task"] . " (Pending)\n";
}
}
?>All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators6Arrays Part 1 - Indexed
Introduction to ArraysCreating Indexed ArraysAccessing Elements by IndexModifying Elements by IndexArray Size with CountAdding Elements to an ArrayRecap - Managing a Simple List9Project: Simple To-Do List
Project Overview & DataAdding a New Task