How can PHP be used for syntax highlighting on a website?
To implement syntax highlighting on a website using PHP, you can utilize libraries like GeSHi or highlight.js. These libraries provide functions to easily highlight code snippets in various programming languages. By including the necessary library files and calling the appropriate functions, you can display code with syntax highlighting on your website.
<?php
// Include the GeSHi library
require_once 'geshi.php';
// Define the code snippet and language
$code = 'echo "Hello, World!";';
$lang = 'php';
// Create a GeSHi object and output the highlighted code
$geshi = new GeSHi($code, $lang);
echo $geshi->parse_code();
?>
Related Questions
- How can PHP be used to dynamically change image sources without directly embedding PHP code in the HTML file?
- What are the best practices for troubleshooting PHP extension activation issues in a web server environment?
- What are best practices for handling special characters like ä, ü, and ö in PHP forms and databases?