Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Fiber.
PHP
<?php
$fiber = new \Fiber(function() {
echo [1];
});
$fiber->start();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using print instead of echo inside the Fiber
Passing a non-string value to echo
✗ Incorrect
The Fiber function should echo a string. The string must be quoted properly in PHP.
2fill in blank
mediumComplete the code to start the Fiber and get its return value.
PHP
<?php
$fiber = new \Fiber(function() {
return 42;
});
$result = $fiber->[1]();
echo $result;
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resume() before start()
Using a non-existent method like run() or begin()
✗ Incorrect
To start a Fiber and get its return value, use the start() method.
3fill in blank
hardFix the error in the Fiber code to yield a value and resume it.
PHP
<?php
$fiber = new \Fiber(function() {
$value = Fiber::[1](10);
echo "Received: $value";
});
$fiber->start();
$fiber->resume(20);
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using yield instead of suspend
Using pause or stop which do not exist
✗ Incorrect
The Fiber::suspend() method pauses the Fiber and sends a value outside.
4fill in blank
hardFill both blanks to create a Fiber that suspends and resumes with values.
PHP
<?php
$fiber = new \Fiber(function() {
$value = Fiber::[1]('start');
echo "Resumed with: $value";
});
$fiber->[2]();
$fiber->resume('resume');
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resume() to start the Fiber
Using start() instead of suspend() inside the Fiber
✗ Incorrect
Use Fiber::suspend() to pause and send a value, and start() to begin the Fiber.
5fill in blank
hardFill all three blanks to create a Fiber that exchanges values twice.
PHP
<?php
$fiber = new \Fiber(function() {
$value1 = Fiber::[1]('first');
echo "Got: $value1\n";
$value2 = Fiber::[2]('second');
echo "Got: $value2\n";
return 'done';
});
$result = $fiber->[3]();
echo "Result: $result";
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resume() inside the Fiber instead of suspend()
Using begin() which does not exist
Using resume() to start the Fiber
✗ Incorrect
Inside the Fiber, use suspend() twice to pause and send values. Use start() to begin the Fiber.