Yield keyword behavior
📖 Scenario: Imagine you have a list of tasks to do, but you want to handle them one by one without loading all at once. Using yield in PHP helps you do this efficiently.
🎯 Goal: You will create a generator function using yield to produce tasks one at a time, then loop through them to display each task.
📋 What You'll Learn
Create an array called
$tasks with exact string values: 'Clean', 'Cook', 'Shop'Create a generator function called
taskGenerator that uses yield to return each task from $tasksUse a
foreach loop with variable $task to iterate over taskGenerator()Print each task inside the loop using
echo💡 Why This Matters
🌍 Real World
Generators are useful when working with large data sets or streams where loading all data at once is not efficient.
💼 Career
Understanding <code>yield</code> helps in writing memory-efficient PHP code, useful in backend development and data processing jobs.
Progress0 / 4 steps