📖 Scenario: You are building a simple PHP script that uses fibers to run two tasks concurrently. Fibers allow you to pause and resume code execution, which helps manage multiple tasks without waiting for each to finish before starting the next.
🎯 Goal: Create two fibers that each count numbers with a pause, then run them concurrently by switching between them. Finally, print the combined output showing the interleaved counting.
📋 What You'll Learn
Create two fibers named fiber1 and fiber2 that each count from 1 to 3 with a pause between counts.
Create a variable output to store the combined counting results as a string.
Use a loop to switch between fiber1 and fiber2 until both finish.
Print the output string showing the interleaved counts.
💡 Why This Matters
🌍 Real World
Fibers help PHP programs handle multiple tasks at once without waiting for each to finish, useful in web servers or background jobs.
💼 Career
Understanding fibers prepares you for writing efficient, concurrent PHP code in modern applications and frameworks.
Progress0 / 4 steps
1
Create two fibers for counting
Create two fibers called fiber1 and fiber2. Each fiber should count from 1 to 3. Inside each fiber, use a for loop with variable i from 1 to 3, and after each count, use Fiber::suspend() to pause the fiber.
PHP
Hint
Use new Fiber(function() { ... }) to create each fiber. Inside, use a for loop and call Fiber::suspend() with a string showing the fiber name and count.
2
Create an output variable
Create a variable called output and set it to an empty string "". This will store the combined results from both fibers.
PHP
Hint
Just write $output = ""; to start with an empty string.
3
Run fibers concurrently and collect output
Use a while loop that runs as long as either fiber1 or fiber2 is not terminated. Inside the loop, check if fiber1 is not terminated, then resume it and append the returned string to output. Do the same for fiber2. Use $fiber1->isTerminated() and $fiber2->isTerminated() to check if fibers finished.
PHP
Hint
Use while with the condition checking both fibers. Inside, resume each fiber if not terminated and add the returned string to output.
4
Print the combined output
Write a print statement to display the output variable.
PHP
Hint
Use print($output); to show the combined counting results.
Practice
(1/5)
1. What is the main purpose of using Fiber in PHP?
easy
A. To speed up database queries automatically
B. To create permanent background threads
C. To pause and resume code execution for concurrency
D. To replace all loops with asynchronous calls
Solution
Step 1: Understand Fiber's role
Fibers allow pausing and resuming code blocks, enabling concurrency without threads.
Step 2: Compare options
Only To pause and resume code execution for concurrency correctly describes Fibers' purpose; others describe unrelated features.
Final Answer:
To pause and resume code execution for concurrency -> Option C
Quick Check:
Fibers = pause and resume concurrency [OK]
Hint: Fibers let you pause and resume code easily [OK]
Common Mistakes:
Thinking Fibers create real threads
Assuming Fibers speed up database automatically
Confusing Fibers with async loops
2. Which of the following is the correct way to start a Fiber in PHP?
easy
A. $fiber->run();
B. $fiber->execute();
C. $fiber->begin();
D. $fiber->start();
Solution
Step 1: Recall Fiber methods
The Fiber class uses start() to begin execution.
Step 2: Check other options
Methods like run(), begin(), and execute() do not exist for Fiber.
Final Answer:
$fiber->start(); -> Option D
Quick Check:
Fiber starts with start() method [OK]
Hint: Use start() to begin Fiber execution [OK]
Common Mistakes:
Using run() instead of start()
Trying to call execute() or begin()
Confusing Fiber methods with thread methods
3. What will be the output of this PHP code using Fiber?
A. Fiber::suspend() cannot be called without a value
B. Fiber must be started with start(), not resume() first
C. Fiber constructor requires a return type declaration
D. Fiber cannot echo strings inside its function
Solution
Step 1: Check Fiber start method usage
Fibers must be started with start() before calling resume().
Step 2: Validate other options
Calling Fiber::suspend() without value is allowed; no return type needed; echo is valid inside Fiber.
Final Answer:
Fiber must be started with start(), not resume() first -> Option B
Quick Check:
start() first, then resume() [OK]
Hint: Always start Fiber with start() before resume() [OK]
Common Mistakes:
Calling resume() before start()
Thinking suspend() needs a value
Expecting Fiber function to require return type
5. You want to run two Fibers concurrently to simulate two tasks. Which approach correctly switches between them to print alternating messages?
$fiber1 = new Fiber(function() {
for ($i = 0; $i < 3; $i++) {
echo "A$i ";
Fiber::suspend();
}
});
$fiber2 = new Fiber(function() {
for ($i = 0; $i < 3; $i++) {
echo "B$i ";
Fiber::suspend();
}
});
// Which code below correctly alternates execution?
A) {
$fiber1->start();
$fiber2->start();
$fiber1->resume();
$fiber2->resume();
}
B) {
$fiber1->start();
$fiber1->resume();
$fiber2->start();
$fiber2->resume();
}
C) {
$fiber1->start();
$fiber2->start();
$fiber2->resume();
$fiber1->resume();
}
D) {
$fiber1->start();
$fiber2->start();
$fiber1->resume();
$fiber1->resume();
}
hard
A. Start both fibers, then alternate resume calls: fiber1->resume(), fiber2->resume()
B. Start and resume fiber1 twice before starting fiber2
C. Start both fibers, then resume fiber2 before fiber1
D. Start both fibers, then resume fiber1 twice without resuming fiber2
Solution
Step 1: Understand Fiber suspension
Each Fiber suspends after printing one message, so to alternate, we must resume them in turn.
Step 2: Analyze options for alternating resumes
Start both fibers, then alternate resume calls: fiber1->resume(), fiber2->resume() starts both fibers, then resumes fiber1 and fiber2 alternately, producing alternating output. Others resume one fiber multiple times or in wrong order.
Final Answer:
Start both fibers, then alternate resume calls: fiber1->resume(), fiber2->resume() -> Option A
Quick Check:
Alternate resume calls for concurrency [OK]
Hint: Alternate resume calls to switch between fibers [OK]
Common Mistakes:
Resuming one fiber multiple times before the other
Starting fibers but not resuming correctly
Resuming fibers in wrong order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.