Introduction
PHP remains one of the most popular languages for web development: WordPress, Laravel, Symfony, and millions of websites run on it. However, with increasing load, the performance of PHP applications can become a bottleneck. Slow page loading not only worsens the user experience but also lowers search engine rankings (Google considers Core Web Vitals).
In this article, we will explore proven PHP optimization methods that will help speed up your application without completely rewriting the code. You will learn about caching, interpreter configuration, database optimization, and profiling.
1. Caching: The First Step to Speed
Caching is the most effective way to reduce response time. Instead of performing heavy operations every time, save the result and serve it from the cache.
1.1. OpCache (PHP 7/8)
PHP is an interpreted language: on each request, it compiles the script into opcode (bytecode). OpCache stores the compiled opcode in RAM, eliminating recompilation. This provides a performance boost of up to 30–50%.
How to enable: Open php.ini and ensure the parameters are configured:
opcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.revalidate_freq=2For production, set opcache.revalidate_freq=0 and clear the cache manually during deployment.
1.2. Data Caching (Redis / Memcached)
If your application makes expensive database queries or external API calls, use in-memory stores.
Example with Redis:
<?php$redis = new Redis();$redis->connect('127.0.0.1', 6379);
$cacheKey = 'user_profile_' . $userId;$profile = $redis->get($cacheKey);
if (!$profile) { // Expensive database query $profile = getUserProfileFromDB($userId); $redis->setex($cacheKey, 3600, serialize($profile)); // TTL 1 hour}
$profile = unserialize($profile);1.3. HTML Caching (Full Page Cache)
For static or rarely updated pages (blog, landing page), you can cache the complete HTML output. In Laravel this is done via middleware, in Symfony via HTTP cache.
2. SQL Query and Database Optimization
Most often, the bottleneck of a PHP application is the database. Slow queries consume server resources.
2.1. Use Indexes
Ensure that columns used in WHERE, JOIN, and ORDER BY have indexes. You can check this using EXPLAIN SELECT ....
Bad query (without index):
SELECT * FROM orders WHERE status = 'pending';Good query (with index):
CREATE INDEX idx_orders_status ON orders (status);SELECT * FROM orders WHERE status = 'pending';2.2. Avoid N+1 Queries
This is a classic ORM mistake: fetching data for each record in a loop.
Problem (Laravel Eloquent):
$users = User::all();foreach ($users as $user) { echo $user->profile->bio; // new query each time}Solution (eager loading):
$users = User::with('profile')->get();foreach ($users as $user) { echo $user->profile->bio; // one query}2.3. Pagination Instead of LIMIT with Large Offset
With LIMIT 10 OFFSET 100000, MySQL still scans 100010 rows. Use cursor-based pagination (search by index):
SELECT * FROM posts WHERE id > $lastId ORDER BY id ASC LIMIT 10;3. Code and Architecture Optimization
Even with an ideal database and cache, poor code c