DEV Community

Cover image for php bottlenecks and performance
Ahmed Raza Idrisi
Ahmed Raza Idrisi

Posted on

php bottlenecks and performance

PHP Performance Bottlenecks + Concepts

🔹 Common PHP Performance Bottlenecks

  1. Loops
  • Heavy nested loops (for inside foreach) slow execution.
  • Example:

     for ($i = 0; $i < 100000; $i++) {
         // expensive operation here
     }
    

✅ Solution: Minimize work inside loops, break early if possible, use built-in functions (array_map, array_filter) when efficient.

  1. Excessive Database Queries (N+1 problem)
  • Example:

     $users = User::all();
     foreach ($users as $user) {
         echo $user->posts->count(); // runs query every time ❌
     }
    

✅ Solution: Use eager loading:

   $users = User::with('posts')->get();
Enter fullscreen mode Exit fullscreen mode
  1. I/O Bottlenecks
  • File read/write in large loops
  • Slow external API calls ✅ Solution: Caching (Redis, Memcached), batch I/O, async workers (queues).

🔹 Opcode Caches (Opcache)

  • PHP is an interpreted language. Normally:

    • Every request = parse → compile → execute.
  • Opcache stores compiled bytecode in memory → saves compilation time.

  • ✅ Enable in php.ini:

  opcache.enable=1
  opcache.memory_consumption=128
  opcache.max_accelerated_files=10000
Enter fullscreen mode Exit fullscreen mode
  • Benefit: Faster response, less CPU load.

🔹 Password Hashing Best Practices

  • Never store plain text or MD5/SHA1 (too weak).
  • Use PHP’s built-in:
  $hash = password_hash("mypassword", PASSWORD_DEFAULT); // bcrypt/argon2
  if (password_verify("mypassword", $hash)) {
      echo "Password correct!";
  }
Enter fullscreen mode Exit fullscreen mode
  • password_hash automatically salts and uses strong algorithms.
  • password_verify safely compares.

Hands-On Benchmarking

📌 Example: Compare for loop vs foreach for arrays.

<?php
$array = range(1, 1000000);

// Benchmark foreach
$start = microtime(true);
$sum = 0;
foreach ($array as $num) {
    $sum += $num;
}
echo "Foreach: " . (microtime(true) - $start) . " seconds\n";

// Benchmark for
$start = microtime(true);
$sum = 0;
for ($i = 0; $i < count($array); $i++) {
    $sum += $array[$i];
}
echo "For: " . (microtime(true) - $start) . " seconds\n";
Enter fullscreen mode Exit fullscreen mode

👉 You’ll see that foreach is generally faster for arrays.
👉 for with count($array) inside loop is slow → store count in variable instead.


📌 Example: Password Hashing

<?php
$password = "supersecret";

// Hashing
$hash = password_hash($password, PASSWORD_DEFAULT);
echo "Hash: $hash\n";

// Verify
if (password_verify("supersecret", $hash)) {
    echo "Login success!\n";
} else {
    echo "Invalid password\n";
}
Enter fullscreen mode Exit fullscreen mode

Interview Prep Review

Common PHP Interview Questions (from Week 1 topics)

  1. What’s new in PHP 8?
  • JIT, Match expressions, Attributes, Nullsafe operator, Named arguments, Constructor property promotion.
  1. Explain the difference between == and ===.
  • == compares values (type juggling).
  • === compares value and type (strict).
  1. How does password_hash differ from md5 or sha1?
  • password_hash is adaptive, salted, secure.
  • MD5/SHA1 are fast → vulnerable to brute force.
  1. What is a PHP Trait?
  • Mechanism for code reuse, allows grouping methods to include in multiple classes without inheritance.
  1. What are Generators (yield)?
  • Functions that return values one at a time without storing the whole dataset in memory → efficient for large data.
  1. Explain SRP (Single Responsibility Principle).
  • A class should have only one reason to change. Helps with maintainability.
  1. What’s the difference between git merge and git rebase?
  • Merge: keeps history with branches.
  • Rebase: rewrites history, keeps linear commit log.

✅ By the end of this:

  • You’ll spot bottlenecks in PHP.
  • You’ll benchmark performance with microtime(true).
  • You’ll be confident answering interview-style questions aloud.

Top comments (0)