PHP Performance Bottlenecks + Concepts
🔹 Common PHP Performance Bottlenecks
- Loops
- Heavy nested loops (
for
insideforeach
) 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.
- 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();
- 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
- 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!";
}
- ✅
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";
👉 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";
}
Interview Prep Review
Common PHP Interview Questions (from Week 1 topics)
- What’s new in PHP 8?
- JIT, Match expressions, Attributes, Nullsafe operator, Named arguments, Constructor property promotion.
- Explain the difference between
==
and===
.
-
==
compares values (type juggling). -
===
compares value and type (strict).
- How does
password_hash
differ frommd5
orsha1
?
-
password_hash
is adaptive, salted, secure. - MD5/SHA1 are fast → vulnerable to brute force.
- What is a PHP Trait?
- Mechanism for code reuse, allows grouping methods to include in multiple classes without inheritance.
- What are Generators (
yield
)?
- Functions that return values one at a time without storing the whole dataset in memory → efficient for large data.
- Explain SRP (Single Responsibility Principle).
- A class should have only one reason to change. Helps with maintainability.
- What’s the difference between
git merge
andgit 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)