What are common pitfalls in PHP coding that can affect website loading speed?

One common pitfall in PHP coding that can affect website loading speed is not optimizing database queries. This can lead to slow loading times as the server has to process a large amount of data unnecessarily. To solve this issue, you can optimize your database queries by using indexes, limiting the number of rows returned, and avoiding unnecessary joins. Example PHP code snippet for optimizing database queries:

// Example of optimizing a database query by adding an index
$query = "SELECT * FROM users WHERE username = 'john'";
// Add an index on the username column for faster retrieval
// ALTER TABLE users ADD INDEX idx_username (username);
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
    // Process the data
}