What alternative approach can be used to display highlighted PHP code with line numbers in a forum post?

When displaying highlighted PHP code with line numbers in a forum post, a common approach is to use a syntax highlighting library like Prism.js or highlight.js. These libraries can automatically add line numbers to the displayed code. Alternatively, you can manually add line numbers to each line of the code before displaying it in the forum post.

// Example PHP code snippet with line numbers
$code = '
1. <?php
2. $name = "John Doe";
3. echo "Hello, $name!";
4. ?>
';

// Display the code with line numbers
$lines = explode("\n", $code);
foreach ($lines as $key => $line) {
    echo ($key + 1) . '. ' . htmlspecialchars($line) . '<br>';
}