How can HTML code execution be prevented in a textarea when displaying content from a file in PHP?

To prevent HTML code execution in a textarea when displaying content from a file in PHP, you can use the htmlspecialchars function to escape any HTML characters. This will ensure that the content is displayed as plain text rather than being interpreted as HTML code.

<?php
$fileContent = file_get_contents('file.txt');
echo '<textarea>' . htmlspecialchars($fileContent) . '</textarea>';
?>