How can the PHP script be optimized for better performance and security?

To optimize a PHP script for better performance and security, you can implement the following best practices: 1. Use prepared statements or parameterized queries to prevent SQL injection attacks. 2. Avoid using deprecated functions and keep your PHP version up to date. 3. Enable output buffering to improve script performance by reducing the number of HTTP requests.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();
$results = $stmt->fetchAll();