In what ways can PHP code be improved for better performance and efficiency when dealing with a large amount of data?

When dealing with a large amount of data in PHP, one way to improve performance and efficiency is by optimizing database queries. This can be done by using indexing, limiting the number of columns retrieved, and using prepared statements to prevent SQL injection attacks.

// Example of optimizing a database query using prepared statements
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch();