PHP Performance Bottlenecks + Concepts
πΉ Common PHP Performance Bottlenecks
- Loops
- Heavy nested loops (
forinsideforeach) 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_hashautomatically salts and uses strong algorithms. - β
password_verifysafely 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_hashdiffer frommd5orsha1?
-
password_hashis 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 mergeandgit 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)