Bird
Raised Fist0
PHPprogramming~20 mins

Fibers for concurrency in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fiber Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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";
A
Inside fiber.
Before fiber start.
Between fiber suspend and resume.
Resumed fiber.
After fiber resume.
B
Before fiber start.
Inside fiber.
Resumed fiber.
Between fiber suspend and resume.
After fiber resume.
C
Before fiber start.
Between fiber suspend and resume.
Inside fiber.
Resumed fiber.
After fiber resume.
D
Before fiber start.
Inside fiber.
Between fiber suspend and resume.
Resumed fiber.
After fiber resume.
Attempts:
2 left
💡 Hint
Remember that Fiber::suspend pauses execution inside the fiber and returns control to the caller.
🧠 Conceptual
intermediate
1:30remaining
Fiber state after suspension
After a Fiber suspends using Fiber::suspend(), what is the state of the Fiber object?
AThe Fiber is terminated and cannot be resumed.
BThe Fiber is in 'suspended' state and can be resumed later.
CThe Fiber is in 'started' state but cannot be resumed again.
DThe Fiber is in 'new' state waiting to start.
Attempts:
2 left
💡 Hint
Think about what Fiber::suspend() does to the fiber's execution.
🔧 Debug
advanced
2: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();
ASyntaxError: Unexpected token 'resume'
BFatal error: Call to undefined method Fiber::resume()
CFiberError: Cannot resume a terminated fiber
DNo error, prints 'Running fiber.' twice
Attempts:
2 left
💡 Hint
Consider what happens when you resume a fiber that already finished.
🚀 Application
advanced
2: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();
A
Fiber1: 1
Fiber2: 1
Fiber1: 2
Fiber2: 2
Fiber1: 3
Fiber2: 3
B
Fiber2: 1
Fiber1: 1
Fiber2: 2
Fiber1: 2
Fiber2: 3
Fiber1: 3
C
Fiber1: 1
Fiber1: 2
Fiber1: 3
Fiber2: 1
Fiber2: 2
Fiber2: 3
D
Fiber1: 1
Fiber2: 1
Fiber2: 2
Fiber1: 2
Fiber1: 3
Fiber2: 3
Attempts:
2 left
💡 Hint
Fibers suspend after each print, so control switches between them.
❓ Predict Output
expert
3: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";
A
Start fiber
Resuming fiber
After suspend
Caught exception: Error inside fiber
Fiber is terminated: yes
B
Start fiber
After suspend
Resuming fiber
Caught exception: Error inside fiber
Fiber is terminated: yes
C
Start fiber
Resuming fiber
Caught exception: Error inside fiber
Fiber is terminated: no
D
Start fiber
Resuming fiber
After suspend
Fiber is terminated: yes
Attempts:
2 left
💡 Hint
Exception inside fiber is caught outside; fiber terminates after exception.

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

  1. Step 1: Understand Fiber's role

    Fibers allow pausing and resuming code blocks, enabling concurrency without threads.
  2. Step 2: Compare options

    Only To pause and resume code execution for concurrency correctly describes Fibers' purpose; others describe unrelated features.
  3. Final Answer:

    To pause and resume code execution for concurrency -> Option C
  4. 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

  1. Step 1: Recall Fiber methods

    The Fiber class uses start() to begin execution.
  2. Step 2: Check other options

    Methods like run(), begin(), and execute() do not exist for Fiber.
  3. Final Answer:

    $fiber->start(); -> Option D
  4. 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?
$fiber = new Fiber(function(): void {
    $value = Fiber::suspend('paused');
    echo "Resumed with: $value";
});

echo $fiber->start();
$fiber->resume('data');
medium
A. pausedResumed with: data
B. Resumed with: datapaused
C. paused
D. Resumed with: data

Solution

  1. Step 1: Analyze Fiber start()

    The start() runs until Fiber::suspend('paused'), which returns 'paused'. So, echo outputs 'paused'.
  2. Step 2: Analyze Fiber resume()

    The resume('data') continues after suspend, echoing "Resumed with: data".
  3. Final Answer:

    pausedResumed with: data -> Option A
  4. Quick Check:

    start() returns suspend value, resume() continues [OK]
Hint: start() returns suspend value, resume() continues execution [OK]
Common Mistakes:
  • Assuming start() returns void
  • Thinking resume() returns suspend value
  • Mixing order of output strings
4. Identify the error in this Fiber code snippet:
$fiber = new Fiber(function() {
    echo "Start\n";
    Fiber::suspend();
    echo "End\n";
});

$fiber->resume();
medium
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

  1. Step 1: Check Fiber start method usage

    Fibers must be started with start() before calling resume().
  2. Step 2: Validate other options

    Calling Fiber::suspend() without value is allowed; no return type needed; echo is valid inside Fiber.
  3. Final Answer:

    Fiber must be started with start(), not resume() first -> Option B
  4. 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

  1. Step 1: Understand Fiber suspension

    Each Fiber suspends after printing one message, so to alternate, we must resume them in turn.
  2. 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.
  3. Final Answer:

    Start both fibers, then alternate resume calls: fiber1->resume(), fiber2->resume() -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard