What are some common challenges faced when trying to display code with line numbers in PHP?

One common challenge when trying to display code with line numbers in PHP is ensuring that the line numbers are accurately displayed and synchronized with the code. To solve this, you can use a loop to iterate through each line of code and increment a line number counter accordingly.

<?php
$code = '<?php echo "Hello, World!"; ?>';
$lines = explode("\n", $code);
$lineNumber = 1;

foreach ($lines as $line) {
    echo $lineNumber . ": " . $line . "<br>";
    $lineNumber++;
}
?>