Is htmlentities() a sufficient protection against code execution in PHP?

htmlentities() is not a sufficient protection against code execution in PHP because it only converts special characters to their HTML entities, preventing XSS attacks. To protect against code execution, you should use functions like htmlspecialchars() or strip_tags() in combination with htmlentities() to sanitize user input before outputting it to the page.

// Sanitize user input to prevent code execution
$userInput = "<script>alert('Hello!');</script>";
$safeInput = htmlentities(strip_tags($userInput));

echo $safeInput;