Challenge - 5 Problems
Fiber Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Fiber switch
What is the output of this PHP code using Fibers?
PHP
<?php $fiber = new Fiber(function(): void { echo "Inside fiber.\n"; Fiber::suspend(); echo "Resumed fiber.\n"; }); echo "Before fiber start.\n"; $fiber->start(); echo "Between fiber suspend and resume.\n"; $fiber->resume(); echo "After fiber resume.\n";
Attempts:
2 left
💡 Hint
Remember that Fiber::suspend pauses execution inside the fiber and returns control to the caller.
✗ Incorrect
The fiber starts and prints "Inside fiber." then suspends. Control returns to main which prints "Between fiber suspend and resume.". Then resume continues fiber printing "Resumed fiber." and finally main prints "After fiber resume.".
🧠 Conceptual
intermediate1:30remaining
Fiber state after suspension
After a Fiber suspends using Fiber::suspend(), what is the state of the Fiber object?
Attempts:
2 left
💡 Hint
Think about what Fiber::suspend() does to the fiber's execution.
✗ Incorrect
Fiber::suspend() pauses the fiber and puts it in a suspended state. It can be resumed later with Fiber::resume().
🔧 Debug
advanced2:00remaining
Identify the error in Fiber usage
What error will this PHP code produce when run?
PHP
<?php $fiber = new Fiber(function(): void { echo "Running fiber.\n"; }); $fiber->start(); $fiber->resume();
Attempts:
2 left
💡 Hint
Consider what happens when you resume a fiber that already finished.
✗ Incorrect
After start(), the fiber runs and terminates. The second resume tries to resume a terminated fiber, causing a FiberError.
🚀 Application
advanced2:30remaining
Using Fibers to simulate cooperative multitasking
Given two fibers that print numbers with suspend calls, what is the output of this code?
PHP
<?php $fiber1 = new Fiber(function() { for ($i = 1; $i <= 3; $i++) { echo "Fiber1: $i\n"; Fiber::suspend(); } }); $fiber2 = new Fiber(function() { for ($i = 1; $i <= 3; $i++) { echo "Fiber2: $i\n"; Fiber::suspend(); } }); $fiber1->start(); $fiber2->start(); $fiber1->resume(); $fiber2->resume(); $fiber1->resume(); $fiber2->resume();
Attempts:
2 left
💡 Hint
Fibers suspend after each print, so control switches between them.
✗ Incorrect
Each fiber prints a number then suspends. The main code resumes them alternately, so output interleaves numbers from fiber1 and fiber2.
❓ Predict Output
expert3:00remaining
Fiber with return value and exception handling
What is the output of this PHP code using Fibers with return and exception?
PHP
<?php $fiber = new Fiber(function(): int { echo "Start fiber\n"; Fiber::suspend(); echo "After suspend\n"; throw new Exception("Error inside fiber"); return 42; }); try { $fiber->start(); echo "Resuming fiber\n"; $fiber->resume(); } catch (Exception $e) { echo "Caught exception: " . $e->getMessage() . "\n"; } echo "Fiber is terminated: " . ($fiber->isTerminated() ? 'yes' : 'no') . "\n";
Attempts:
2 left
💡 Hint
Exception inside fiber is caught outside; fiber terminates after exception.
✗ Incorrect
The fiber starts and prints "Start fiber" then suspends. Main resumes fiber, which prints "After suspend" then throws exception caught outside. Fiber is terminated after exception.