Asynchronous Programming in PHP: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Asynchronous Programming in PHP

Traditionally, PHP is considered a synchronous language: each request is executed sequentially, blocking execution until an operation (e.g., a database or API request) completes. However, with growing performance demands for web applications, asynchronous programming in PHP is becoming increasingly popular. Asynchronous code allows multiple tasks to be executed simultaneously without waiting for each to complete, significantly speeding up request processing and reducing server load.

In this guide, we will break down the core concepts of asynchronous programming in PHP, look at popular libraries (ReactPHP, Amp), as well as modern language features — Fiber and generators.



Asynchrony Basics: Event Loop and Non-blocking I/O

At the heart of any asynchronous approach lies the Event Loop. This is an infinite loop that checks whether any event has occurred (e.g., an HTTP request has finished or a file read has completed). If an event occurs, the corresponding handler is executed. Meanwhile, the main thread is not blocked — it can switch to another task.

Non-blocking I/O is a key principle. In synchronous code, a call to file_get_contents() blocks script execution until the file is fully loaded. In asynchronous mode, you initiate the operation and continue executing other code, with the result arriving via a callback or Promise.



Example: Synchronous vs Asynchronous Request

// Synchronous code (blocking)$start = microtime(true);$data1 = file_get_contents('http://example.com/api1');$data2 = file_get_contents('http://example.com/api2');echo "Execution time: " . (microtime(true) - $start) . " sec"; // ~2 sec (if each request takes 1 sec)

// Asynchronous code (ReactPHP)$loop = React\\EventLoop\\Factory::create();$client = new React\\Http\\Browser($loop);

$promise1 = $client->get('http://example.com/api1');$promise2 = $client->get('http://example.com/api2');

$promise1->then(function ($response) { echo "Response 1 received";});$promise2->then(function ($response) { echo "Response 2 received";});

$loop->run();// Execution time ~1 sec (requests are executed in parallel)


Libraries for Asynchronous Programming in PHP

Today, the two most popular libraries are ReactPHP and Amp. Both provide an Event Loop, Promises, and ready-made components for working with HTTP, sockets, and the file system.



ReactPHP

ReactPHP is a mature library that has existed since 2012. It provides low-level access to the event loop and many components: react/socket, react/http, react/dns, and others.

// Example of an HTTP server using ReactPHP$loop = React\\EventLoop\\Factory::create();$server = new React\\Http\\Server($loop, function (Psr\\Http\\Message\\ServerRequestInterface $request) {    return new React\\Http\\Message\\Response(        200,        ['Content-Type' => 'text/plain'],        "Hello, Async World!"    );});$socket = new React\\Socket\\Server('127.0.0.1:8080', $loop);$server->listen($socket);echo "Server started on http://127.0.0.1:8080";$loop->run();


Amp

Amp uses coroutines (generator-based coroutines) and provides a higher-level API. Instead of then() chains, you use yield and await, making the code resemble synchronous code.

// Example with Amp\\Amp\\Loop::run(function () {    $response = yield \\Amp\\Http\\Client\\HttpClient::request('http://example.com');    $body = yield $response->getBody()->buffer();    echo $body;});


Fiber: Asynchrony in the PHP 8.1 Core

Starting with PHP 8.1, the language added

Blogs

Book Recommendations