Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Concepts and Reference

PHP performance tips

Practical PHP performance advice: measure first, use OPcache, avoid repeated work, choose the right data structure, stream data and cut database round trips.

Most slow PHP applications are slow because of the database, the network or an algorithm, not because of PHP syntax. The most valuable habit is to measure first, then change the biggest cost. Micro-optimizations such as single versus double quotes make no practical difference.

This page collects the practices that regularly pay off, with two small experiments you can run. Timings are not printed because they vary between machines; the examples print results that do not depend on speed.

PHP
<?php
$start = hrtime(true);
// ... code under test ...
$ms = (hrtime(true) - $start) / 1e6;

Try it

PHP Lookups: in_array() scans, isset() hashes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Avoid repeated work: cache inside the function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

What to do

Area Advice
Runtime Enable OPcache in production. Keep PHP current: each 8.x release is faster than the last.
Database Fix N+1 queries, add indexes for WHERE and JOIN columns, select only needed columns, paginate.
Algorithms Use arrays keyed by id for lookups instead of searching lists. Sort once.
Memory Use generators or streams for large data. Free big arrays with unset() when done.
I/O Batch writes, avoid reading the same file repeatedly, cache API results.
Autoloading Use Composer's optimized classmap (composer dump-autoload -o) in production.
Caching Cache expensive results (APCu, Redis, files) with a clear expiry.
Profiling Use Xdebug profiler, Blackfire or SPX to see real hot spots.

Usage notes

  • Avoid calling functions in a loop condition when the result does not change (for ($i = 0; $i < count($a); $i++) is fine in modern PHP, but a heavy call is not).
  • Build strings with implode() or output buffering rather than thousands of tiny concatenations in tight loops over huge data.
  • array_key_exists() and isset() are both fast; isset() returns false for null values.
  • Passing arrays to functions is cheap thanks to copy-on-write. Do not use references just to "save memory".
  • Regular expressions with nested quantifiers can be catastrophically slow. Test them on bad input.
  • JIT (enabled through OPcache) helps CPU-heavy code such as math loops, but typical web requests spend their time waiting on I/O and gain little.
  • Readable code that is fast enough beats clever code that is faster by 2%.