For many years, PHP has been one of the most popular programming languages for web development. In fact, some estimates suggest that PHP powers around 70% of all websites. Why? This popularity can be attributed to two main factors:
However, over the past decade, other solutions such as Node.js and Golang have emerged, promising a new level of performance. With that in mind, the big questions are: Where does PHP stand in this competition? What does PHP offer in the new web era, where scalability and performance are key requirements?
In today’s post, I’ll guide you through traditional PHP and the advancements it has seen in recent years. By the end of this article, you’ll understand how to expertly leverage PHP for web development.
Let’s dive in.
Nginx and PHP-FPM are the most common execution environments for PHP applications today. Some years ago, Apache HTTPd and mod_php were widely used, and many legacy applications still rely on this setup. Both solutions share a common trace: they execute PHP in a stateless manner.
Now, what’s stateless? For simplicity, stateless means that your application does not retain state between requests. Basically:
Using this architecture is easy for beginner developers since they don’t have to worry about state management, and last but not least, memory leaks are less likely.
It is not all roses, however. As you might imagine, this approach is expensive in terms of computing resources. It requires reconstructing every resource required for each request. Imagine that for every incoming request, you need to open a database connection, load environment variables, instantiate a cache driver, and initialize various other dependencies. This process happens repeatedly for each request, even if nothing has changed.
This challenge is one of the primary areas addressed by the latest runtimes that have emerged in recent years, alongside concurrency and non-blocking I/O. But before diving into these, it’s imperative to understand the computing era we are currently in, the one where concurrency is key.
Concurrency is the capability of different parts of a software system to execute independently, without interfering with the final outcome. Think of it as a way to scale horizontally, allowing processes to run across multiple CPUs, cores, or even different machines. This technique splits workloads to be split into separate workers, enabling tasks to be processed simultaneously.
At this point, you might be wondering why concurrency is essential now. Here are two key reasons:
However, to fully leverage these resources, modern software needs to be designed for concurrency. Programming languages should offer simpler ways to achieve it, enabling developers to break down tasks into concurrent operations that can be executed in parallel.
Overall, concurrency is changing how we approach software design. We’re moving away from single-threaded, sequential processes to models that can handle multiple tasks at once. This shift is especially beneficial in utilizing multicore processors for parallelism.
In the next section, I’ll explore how modern PHP web servers leverage concurrency and stateful execution to enhance application performance. By understanding and implementing these concepts, developers can elevate their PHP applications to new levels of efficiency and scalability.
You’ve now seen the limitations of traditional PHP and the importance of concurrency. Let’s see two modern PHP web servers making waves in the developer community.
Two of the most discussed modern PHP web servers in recent years are RoadRunner and FrankenPHP.
Both servers are written in Go and leverage its built-in concurrency features to serve PHP. The core concept is simple: these servers create workers using goroutines (Go’s lightweight threads), which handle requests concurrently.
A key advantage of these servers is their ability to keep the application state in memory, which was previously uncommon in the PHP ecosystem. When running PHP on these servers, the application is instantiated once by the worker, allowing each request to reuse resources already loaded in memory.
This approach, combined with concurrent request processing, leads to significant performance improvements. For example, FrankenPHP claims to be up to 3.5 times faster than PHP-FPM when using the stateful approach with workers processing requests concurrently.
Similarly, RoadRunner showcases even more dramatic performance gains. Benchmarks show that its response times significantly outperform other PHP web servers, including PHP-FPM. These results highlight how maintaining applications in memory and leveraging concurrency can substantially boost PHP performance.
By adopting RoadRunner or FrankenPHP, developers can achieve faster and more efficient PHP applications, making these servers an outstanding choice for modern web development.
Swoole is a PHP extension written in C/C++, the same language as PHP itself. It introduces concurrency to PHP, keeps the application in memory when running a web server (similar to FrankenPHP and Roadrunner), and brings a non-blocking I/O model to PHP. Swoole is a game-changer for serving PHP web applications.
Swoole’s concurrency model is similar to Go’s but is natively PHP. Yes, you can write concurrent code using PHP! Swoole’s concurrency is based on the famous Communication Sequential Process (CSP), bringing the concepts of Coroutines and Channels to PHP.
In addition to coroutines, Swoole introduces a non-blocking I/O model for PHP. This means that for network I/O operations like database queries or HTTP requests, CPU resources are not allocated just to wait for memory. Instead, the resource can be used to process another coroutine while the response is not ready.
Combining coroutines with a non-blocking I/O model results in remarkable performance gains. Swoole’s documentation provides an eye-opening example:
“How many things can you do in one second?”. According to it, with Swoole, you can perform a one-second sleep 10,000 times, 10,000 file I/O operations, 10,000 database operations, and manage a server with multiple clients communicating 10,000 times—all in just one second.
These concepts are applied to Swoole’s built-in web server. Each incoming request is treated as a coroutine that leverages the non-blocking I/O model to process the request efficiently. The result is as expected. According to Swoole’s documentation, its built-in web server significantly outperforms traditional PHP-FPM, as well as the built-in HTTP servers of Golang and Node.js
Beyond just web servers, Swoole also includes TCP/UDP servers, WebSockets, IoT applications, and more, making it a robust choice for a wide range of use cases in modern web development using PHP.
For developers, tech enthusiasts, and software engineers looking to enhance their PHP applications, Swoole offers significant technical benefits. Its concurrency model, non-blocking I/O, and remarkable performance gains make it an essential tool in modern web development.
PHP has evolved significantly in recent years, not only through core performance improvements introduced in PHP 7 and PHP 8 but also thanks to community-driven innovations. Tools like FrankenPHP, RoadRunner, and Swoole exemplify this evolution, pushing PHP beyond its traditional boundaries.
Once known for its simplicity and stateless execution model, PHP is now equipped to meet the demands of concurrency and high performance in modern web applications. This means developers now have the tools to achieve exceptional results.
However, having these powerful tools at our disposal is just the beginning. The onus is on developers to learn how to fully harness the capabilities that PHP and its ecosystem provide. This includes exploring the design and development of code that can be executed concurrently, whether in cloud environments or on multicore processors.
As Herb Sutter stated in his 2005 article, The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software: “The vast majority of programmers today don’t grok concurrency, just as the vast majority of programmers 15 years ago didn’t yet grok objects.”
Nearly two decades later, this statement still holds true.
Are you, as a PHP developer, ready to tackle these new challenges? If not, it’s time to deepen your understanding of concurrency, non-blocking I/O, PHP stateful application design, and the evolving ecosystem.
Elevate your development skills to fully leverage what modern PHP has to offer.
In the same way that big players do, offshore staff augmentation in tech is a strategic move to expand your in-house team. By leveraging top performers’ experience from around the world, you can incorporate specialized knowledge in less time and accelerate your project’s growth. But it’s not without risks. Onboarding the most suitable offshore software…
Meet Bruno Feitoza, Senior Software Engineer at BEON.tech, currently wrangling code for one of our partners in Houston, Texas. Besides being a coding wizard, Bruno is incredibly approachable and persistent. His optimism and strong sense of community are something we genuinely appreciate, and they’re part of what makes him shine in his career. During our…
Let me be direct: Argentina’s software developers are revolutionizing the tech industry. A few days ago, Jordan Belfort, a well-known behemoth in the sales industry, shared on his X account: “Just returned from Argentina and, as always, I’m absolutely blown away! Such a promising country with endless possibilities. Already planning my next trip back!” But…