What are some common pitfalls when working with PHP highlighting in BBCode?

One common pitfall when working with PHP highlighting in BBCode is that the PHP code may not be properly escaped, leading to syntax errors or security vulnerabilities. To solve this issue, it is important to properly escape the PHP code using htmlentities() or htmlspecialchars() functions before displaying it in the BBCode.

// Escaping PHP code before displaying in BBCode
$phpCode = "<?php echo 'Hello, World!'; ?>";
$escapedPhpCode = htmlentities($phpCode);
echo "[php]" . $escapedPhpCode . "[/php]";