How can PHP scripts be optimized to improve security and performance?

To optimize PHP scripts for improved security and performance, developers can follow best practices such as using parameterized queries to prevent SQL injection, validating and sanitizing user input, using prepared statements, enabling output buffering, caching data where appropriate, and minimizing the use of global variables.

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