PHP Performance Bottlenecks + Concepts
- Loops: Loops are like a never-ending PHP performance bottleneck! They can be incredibly slow, especially when nested or if the loop is excessive in size. Here’s how to minimize their impact:
- Minimize work inside loops: Break early if possible, use built-in functions like
array_map
andarray_filter
to perform efficient operations on the loop data. - Excessive Database Queries (N+1 problem): Use eager loading: Fetch data from the database in a single request, rather than performing the query every time the loop iterates. This can significantly reduce the number of database queries and improve performance.
- Minimize work inside loops: Break early if possible, use built-in functions like
- Excessive Database Queries (N+1 problem): PHP is a
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 vsforeach
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
withcount($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.