Common PHP Performance Optimization and Tuning Techniques
This article presents several practical PHP performance optimization methods—including caching, avoiding duplicate database queries, merging and compressing assets, using MySQL query cache, and efficient database operations—accompanied by clear code examples to help developers improve web application speed.
When developing web applications, performance optimization is essential, and PHP offers various techniques and tools to enhance speed. This article introduces common PHP performance optimization and tuning methods with example code.
Using Cache
Caching reduces database access and I/O operations. PHP's built-in functions apc_add() and apc_fetch() can implement simple caching.
// Cache key
$key = 'my_cache_key';
// Try to fetch data from cache
$data = apc_fetch($key);
if ($data === false) {
// Data not in cache, perform DB query or other operations
// ...
// Store result in cache for 60 seconds
apc_add($key, $data, 60);
}
// Use $data
// ...Avoid Duplicate Database Queries
Repeated identical queries waste resources; using static or global variables can cache results.
function get_user_count() {
static $count = null;
if ($count === null) {
// Execute DB query
$count = // ...
}
return $count;
}Merge Files and Compress Resources
Reducing HTTP requests improves performance; combine multiple CSS/JS files and compress them.
function compress_css($files, $output_file) {
$content = '';
foreach ($files as $file) {
$content .= file_get_contents($file);
}
$content = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $content);
$content = str_replace(["
","","
"," "," "," "," "], '', $content);
file_put_contents($output_file, $content);
}Using Query Cache
For frequently executed identical queries, MySQL's query cache can be enabled with SELECT SQL_CACHE.
$sql = "SELECT SQL_CACHE * FROM my_table WHERE ...";Using Efficient Database Operations
Optimizing DB operations with indexes, batch inserts, and batch updates boosts efficiency.
// Use index
$sql = "SELECT * FROM my_table WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
// Batch insert
$sql = "INSERT INTO my_table (id, name) VALUES (:id, :name)";
$stmt = $pdo->prepare($sql);
foreach ($data as $row) {
$stmt->bindParam(':id', $row['id'], PDO::PARAM_INT);
$stmt->bindParam(':name', $row['name'], PDO::PARAM_STR);
$stmt->execute();
}
// Batch update
$sql = "UPDATE my_table SET name = :name WHERE id = :id";
$stmt = $pdo->prepare($sql);
foreach ($data as $row) {
$stmt->bindParam(':id', $row['id'], PDO::PARAM_INT);
$stmt->bindParam(':name', $row['name'], PDO::PARAM_STR);
$stmt->execute();
}Conclusion
The article covered common PHP performance optimization methods such as proper caching, reducing database queries, merging files, using query cache, and optimizing database operations. Readers are encouraged to select suitable techniques for their scenarios and apply the provided examples to achieve better performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
