🐘 PHP Intermediate

What is PHP Fibers?

Answer

Fibers (PHP 8.1) are units of concurrency that allow functions to be paused and resumed. They are PHP's implementation of coroutines at the language level. A Fiber can be paused mid-execution with Fiber::suspend($value) and resumed with $fiber->resume($value). Unlike threads, Fibers are cooperative — they yield control explicitly and run in a single thread. They enable non-blocking I/O patterns and are the foundation for asynchronous PHP frameworks like ReactPHP and Amp (v3+). Example: $fiber = new Fiber(function(): void { $value = Fiber::suspend("paused"); }); $result = $fiber->start(); $fiber->resume("hello");. Fibers are lower-level than async/await but enable async libraries to be built on top of them.