What are the best practices for optimizing PHP scripts to ensure efficient execution, especially when transitioning from PHP 4 to PHP 5?

To optimize PHP scripts for efficient execution when transitioning from PHP 4 to PHP 5, it is important to update deprecated functions, utilize object-oriented programming, and enable opcode caching. Additionally, using proper error handling techniques and optimizing database queries can also improve performance.

// Update deprecated functions
// Before: mysql_connect()
// After: mysqli_connect()

// Utilize object-oriented programming
class Database {
    private $connection;

    public function __construct($host, $username, $password, $database) {
        $this->connection = new mysqli($host, $username, $password, $database);
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }
}

// Enable opcode caching in php.ini
// e.g., opcache.enable=1

// Proper error handling
try {
    // Code that may throw an exception
} catch (Exception $e) {
    // Handle the exception
}

// Optimize database queries
// Use prepared statements to prevent SQL injection
$stmt = $connection->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Process the data
}