How can PHP developers prevent the execution of JavaScript code when displaying PHP code in a forum?

To prevent the execution of JavaScript code when displaying PHP code in a forum, PHP developers can use the htmlentities function to encode any HTML and JavaScript characters in the PHP code before displaying it on the forum. This will ensure that the code is displayed as plain text and not executed as JavaScript.

<?php
$phpCode = "<script>alert('Hello, world!');</script>";
echo htmlentities($phpCode);
?>