0
0
PHPprogramming~10 mins

Fibers for concurrency in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Fibers for concurrency
Start main program
Create Fiber
Start Fiber
Fiber runs until yield
Main resumes, does work
Resume Fiber
Fiber continues or ends
Main finishes
The main program creates and starts a fiber, which runs until it yields control back. The main program can then do other work and resume the fiber later, enabling concurrency.
Execution Sample
PHP
<?php
$fiber = new Fiber(function() {
    echo "Fiber started\n";
    Fiber::suspend();
    echo "Fiber resumed\n";
});
$fiber->start();
echo "Main after start\n";
$fiber->resume();
This code creates a fiber that prints a message, suspends itself, then prints another message when resumed. The main program starts the fiber, prints a message, then resumes the fiber.
Execution Table
StepActionOutputFiber StateMain State
1Create FiberCreatedReady
2Start FiberFiber startedSuspended (yielded)Running
3Main printsMain after startSuspended (yielded)Running
4Resume FiberFiber resumedTerminatedRunning
5EndTerminatedFinished
💡 Fiber terminates after finishing its function; main program ends after resuming fiber.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
$fiber stateNot createdSuspended (yielded)TerminatedTerminated
Key Moments - 2 Insights
Why does the fiber stop after the first echo and not run the second echo immediately?
Because at step 2 in the execution_table, the fiber calls Fiber::suspend(), which pauses the fiber and returns control to the main program before the second echo.
What happens when the main program calls resume() on the fiber?
At step 4, resume() continues the fiber from where it was suspended, so it prints "Fiber resumed" and then the fiber ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the fiber state after step 2?
ACreated
BSuspended (yielded)
CTerminated
DRunning
💡 Hint
Check the 'Fiber State' column in row for step 2.
At which step does the main program print its message?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Output' columns in the execution_table.
If Fiber::suspend() was removed, what would happen to the fiber state after start()?
AFiber would be terminated immediately
BFiber would be created but not started
CFiber would be suspended
DFiber would throw an error
💡 Hint
Without suspend, fiber runs to end on start(), see how suspend affects state in execution_table step 2.
Concept Snapshot
Fibers in PHP allow pausing and resuming code blocks.
Create a Fiber with new Fiber(function() {...}).
Start it with start(), which runs until suspend().
Use suspend() to pause and resume() to continue.
This enables cooperative concurrency without threads.
Full Transcript
This example shows how PHP fibers work for concurrency. The main program creates a fiber that prints "Fiber started" and then suspends itself. The main program continues and prints "Main after start". Later, the main program resumes the fiber, which prints "Fiber resumed" and then ends. The fiber's state changes from created to suspended to terminated as it runs. This allows the main program and fiber to cooperate by yielding control back and forth.