Introduction to Asynchronous Programming in PHP
Traditionally, PHP works in synchronous mode: each request is executed sequentially, line by line. However, modern web development increasingly requires handling many tasks simultaneously — from sending emails to working with WebSocket and APIs. Asynchronous programming in PHP allows you to solve these tasks efficiently without blocking the main execution thread.
In this guide, we will break down what asynchrony is, why it is needed, and how to start using it in PHP. You will learn about key tools: Fiber, ReactPHP, Swoole, and built-in functions for working with coroutines.
What is Asynchronous Programming and Why Do You Need It?
Synchronous vs Asynchronous Code
Imagine you are ordering a pizza. The synchronous approach: you call, wait for it to be prepared, and only then hang up. Asynchronous: you call, place the order, and while the pizza is being prepared — you do your own things. In PHP, it looks like this:
// Synchronous code (blocking)$data = file_get_contents('https://api.example.com');echo "Data received: " . $data; // Wait for it to load
// Asynchronous code (non-blocking)$promise = async_file_get_contents('https://api.example.com');echo "Request sent, can do other things...";$data = await($promise); // Get the result laterProblems with Synchronous PHP
- I/O Blocking: operations with files, databases, or network requests stop script execution
- Low Performance: each new request requires a separate process or thread
- Real-time Issues: chats, notifications, and streaming work inefficiently
Advantages of the Asynchronous Approach
- Simultaneous handling of hundreds and thousands of connections
- Memory and server resource savings
- Ability to create real-time applications
- More responsive interfaces
Key Tools for Asynchronous PHP
1. Coroutines and Fiber (PHP 8.1+)
Starting with PHP 8.1, built-in support for Fiber — lightweight coroutines — has been introduced. Fiber allows you to pause function execution and resume it later without blocking the main thread.
<?php$fiber = new Fiber(function(): void { $value = Fiber::suspend('first'); echo "Resumed with value: $value";});
$value = $fiber->start();echo "Initial value: $value"; // first$fiber->resume('second'); // Resumed with value: second?>2. ReactPHP — Event Loop for PHP
ReactPHP is a library that implements event-driven programming (event loop). It allows you to perform non-blocking I/O operations.
<?phprequire 'vendor/autoload.php';
$loop = React\\EventLoop\\Loop::get();
$loop->addTimer(1.0, function () { echo "1 second passed";});
$loop->addTimer(2.0, function () { echo "2 seconds passed";});
echo "Start...";$loop->run();?>ReactPHP also provides asynchronous HTTP clients and servers:
<?php$browser = new React\\Http\\Browser($loop);
$browser->get('https://api.example.com')->then( function (Psr\\Http\\Message\\ResponseInterface $response) { echo "Response: " . $response->getBody(); }, function (Exception $e) { echo "Error: " . $e->getMessage(); });?>3. Swoole — High-Performance Asynchronous Server
Swoole is a PHP extension that turns it into a full-fledged asynchronous server. It supports coroutines, WebSocket, TCP/UDP servers, and much more.
<?ph