0
0
PHPprogramming~10 mins

Fibers for concurrency in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"Hello from Fiber!"
BHello from Fiber!
Cprint("Hello")
Decho 'Hello'
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
2fill in blank
medium

Complete 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'
Astart
Brun
Cbegin
Dresume
Attempts:
3 left
💡 Hint
Common Mistakes
Using resume() before start()
Using a non-existent method like run() or begin()
3fill in blank
hard

Fix 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'
Apause
Bsuspend
Cyield
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using yield instead of suspend
Using pause or stop which do not exist
4fill in blank
hard

Fill 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'
Asuspend
Bstart
Cresume
Dbegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using resume() to start the Fiber
Using start() instead of suspend() inside the Fiber
5fill in blank
hard

Fill 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'
Asuspend
Bresume
Cstart
Dbegin
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