php runtime and server environment

Event Loop PHP

An event loop is a long-running loop that waits for work to become ready, then runs the matching callback. It is common in systems that handle sockets, timers, streams, and many concurrent I/O operations without creating a new process for each task.

PHP is often used through PHP-FPM, where each request feels isolated and short-lived. Event-loop PHP is different: the process stays alive, callbacks run over time, and blocking one callback can delay everything else in that loop.

A tiny mental model

PHP example
<?php

declare(strict_types=1);

$queue = [
    static fn () => 'read from socket',
    static fn () => 'write response',
    static fn () => 'run timer callback',
];

while ($task = array_shift($queue)) {
echo $task() . PHP_EOL;
}

// Prints:
// read from socket
// write response
// run timer callback

This is not a real event loop, but it gives the idea: work is queued, the loop takes ready work, and the process continues until there is no more work or it is stopped.

Real event loops watch file descriptors, sockets, timers, signals, and scheduled callbacks. Libraries such as ReactPHP and Amp provide those pieces so application code does not have to build them from scratch.

Blocking versus non-blocking

The most important beginner concept is blocking. A blocking operation makes the current process wait.

PHP example
<?php

declare(strict_types=1);

echo 'Before blocking work' . PHP_EOL;
sleep(1);
echo 'After blocking work' . PHP_EOL;

// Prints:
// Before blocking work
// After blocking work

In a normal CLI script, this is boring. In an event loop, a blocking sleep, file read, database call, or HTTP request can stop every other task in the same loop from making progress.

Non-blocking code starts work, returns control to the loop, and continues when the result is ready. That is the model ReactPHP and Amp are designed around.

ReactPHP and Amp

ReactPHP provides event-loop components, promises, streams, sockets, HTTP clients, and servers. It is often used when a project wants event-driven PHP components that can run in long-lived CLI processes.

Amp provides async tools using fibers in modern PHP, with libraries for HTTP, sockets, cancellation, synchronization, and concurrency. It aims to make async PHP read more like normal structured code.

You do not need to memorise both ecosystems as a junior. You should recognise what problem they solve: keeping a PHP process alive while it waits on many I/O tasks efficiently.

Where event loops appear

Event-loop PHP may appear in:

  • WebSocket servers
  • chat systems and live dashboards
  • long-running queue consumers
  • HTTP clients making many concurrent requests
  • local development servers and application servers
  • daemons that watch sockets, files, or timers

It is not the default choice for every PHP web application. For many CRUD applications, PHP-FPM plus queues and a normal database is simpler and easier to operate.

Practical cautions

Event-loop code raises the same long-running-process issues as worker runtimes:

  • do not keep request-specific state in globals or static properties
  • avoid blocking calls inside callbacks
  • handle failed connections and retries deliberately
  • clean up timers, listeners, streams, and subscriptions
  • monitor memory growth
  • stop workers gracefully on deploy

The harder part is not starting a loop. The harder part is making sure the code inside the loop behaves well for hours or days.

What you should be able to do

After this lesson, you should be able to explain what an event loop is, why blocking calls are dangerous inside it, where ReactPHP and Amp fit, and when event-loop PHP is more complexity than a project needs.

Practice

Task: Explain Blocking In An Event Loop

Create a small PHP example or review note that explains why blocking work is dangerous inside an event loop.

Requirements

  • Include a tiny queue or callback example that runs multiple tasks.
  • Show where a blocking call would delay later work.
  • Explain the difference between this simple example and a real event-loop library.
  • Name one situation where ReactPHP or Amp could be useful.
  • Name one situation where PHP-FPM would be simpler.

Check your work

The answer should make the event-loop tradeoff understandable without requiring a full async framework installation.

Show solution
PHP example
<?php

declare(strict_types=1);

$tasks = [
    static function (): string {
        return 'read socket';
    },
    static function (): string {
        sleep(1);
        return 'slow blocking task';
    },
    static function (): string {
        return 'run timer callback';
    },
];

foreach ($tasks as $task) {
    echo $task() . PHP_EOL;
}

// Prints:
// read socket
// slow blocking task
// run timer callback

The sleep(1) blocks the process, so the timer callback cannot run until the slow task finishes. A real event-loop library such as ReactPHP or Amp uses non-blocking I/O and scheduling primitives instead of this simple array loop.

ReactPHP or Amp can be useful for a long-running process that manages many sockets or many concurrent HTTP requests. PHP-FPM is simpler for a standard CRUD application where each browser request can be handled independently and long work can be moved to a queue.