Are there any security considerations to keep in mind when using PHP to manipulate and display data on a webpage?

When using PHP to manipulate and display data on a webpage, it is important to sanitize user input to prevent SQL injection and cross-site scripting attacks. One way to do this is by using prepared statements for database queries and escaping output when displaying data to the user.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->execute(['username' => $_POST['username']]);
$user = $stmt->fetch();

// Example of escaping output to prevent cross-site scripting
echo htmlspecialchars($user['username']);