📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Fibers

Fibers

6 min read
Fibers can pause and resume code execution inside a single PHP thread. They are used in async frameworks for cooperative multitasking without OS threads. This is an advanced feature for high-concurrency applications.

Fibers (PHP 8.1)

Fibers are lightweight coroutines for cooperative multitasking.

$fiber = new Fiber(function(): string {
    $value = Fiber::suspend("paused");
    echo "Resumed with: $value\n";
    return "done";
});

// Start fiber — runs until first suspend
$paused = $fiber->start();
echo $paused;  // "paused"

// Resume fiber
$result = $fiber->resume("hello");
// Output: Resumed with: hello

echo $fiber->getReturn(); // "done"

// Fibers are useful for async frameworks (ReactPHP, Swoole)