📡 You're offline — showing cached content
New version available!
Quick Access
PHP Beginner

PHP Performance Tips: Write Faster PHP Code

PHP performance — OpCache, fixing N+1 queries, APCu caching, and profiling with Xdebug.

EzyCoders Admin June 8, 2026 2 min read 17 views
PHP Performance Tips: Write Faster PHP Code
Share: Twitter LinkedIn WhatsApp

What is it?

PHP application performance comes down to a small number of high-impact areas: bytecode caching with OpCache, eliminating redundant database queries, and caching expensive results. These optimisations can make a slow app 10x faster.

Why does it matter?

Slow PHP costs money — server bills increase with load, and users abandon sites that take more than 2 seconds. Most performance problems trace back to a handful of well-known issues with well-known fixes.

PHP performance — OpCache, fixing N+1 queries, APCu caching, and profiling with Xdebug.

Real-World Use Cases

  • 🔥 High-traffic product pages - Enable OpCache and cache the product list in APCu — serve thousands of requests without hitting the database every time.
  • 🗄️ Dashboard with multiple queries - Fix N+1 by rewriting nested loops into a single JOIN query — reduce 101 queries to 1.
  • 📧 Bulk email campaigns - Batch-process 10,000 emails using a while loop with fetch() instead of loading all rows into memory at once.
  • 📊 Report generation - Cache expensive aggregate queries in Redis/APCu for 5 minutes — no need to SUM millions of rows on every page load.

Enable OpCache — Instant 2-10x Speedup

php.ini

opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=60

Restart Apache/PHP-FPM.

Check:

<?php
var_dump(opcache_get_status());

Fix N+1 Queries — The Biggest Win

❌ Bad:

$users = $db->query("SELECT * FROM users");

foreach ($users as $user) {
    $orders = $db->query("SELECT * FROM orders WHERE user_id={$user['id']}");
}

✅ Good:

 

SELECT u.*, o.*
FROM users u
LEFT JOIN orders o ON o.user_id = u.id;

 

APCu Caching — Avoid Repeated DB Calls

$key = 'settings';

$data = apcu_fetch($key);

if ($data === false) {
    $data = $db->query("SELECT * FROM settings")->fetchAll();
    apcu_store($key, $data, 3600);
}

Clear cache:

apcu_clear_cache();

Performance priority

  1. Fix N+1 queries ⭐⭐⭐⭐⭐
  2. Add proper DB indexes ⭐⭐⭐⭐⭐
  3. Enable OPcache ⭐⭐⭐⭐
  4. APCu caching ⭐⭐⭐⭐
  5. Redis for shared cache/sessions ⭐⭐⭐⭐
  6. CDN for static files ⭐⭐⭐

Usually, fixing N+1 queries and enabling OPcache gives the largest immediate improvement.

Q: What is the single biggest PHP performance win?

Enabling OpCache. It makes PHP 2-10x faster with zero code changes. The second biggest win is fixing N+1 database queries with JOINs. Use EXPLAIN in MySQL to identify slow queries and add indexes.

EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment