函数名:highlight_file()
适用版本:PHP 4, PHP 5, PHP 7
用法:highlight_file() 函数用于将文件的内容以语法高亮的方式输出到浏览器或保存到变量中。
语法:bool highlight_file ( string $filename, bool $return = false )
参数:
- $filename:必需,要高亮显示的文件的路径。
- $return:可选,指定是否将高亮后的内容保存到变量中,默认为 false,表示直接输出到浏览器。
返回值:如果 $return 参数为 true,则返回高亮后的内容,否则返回布尔值 true。
示例:
<?php
$filename = "test.php";
// 直接将高亮后的内容输出到浏览器
highlight_file($filename);
// 将高亮后的内容保存到变量
$highlightedCode = highlight_file($filename, true);
echo $highlightedCode;
?>
以上示例中,假设当前目录下存在一个名为 "test.php" 的文件。highlight_file() 函数会读取该文件的内容,并将其以语法高亮的形式输出到浏览器或保存到变量中。如果将 $return 参数设置为 true,则可以将高亮后的内容保存到变量 $highlightedCode 中,并在后续代码中进行其他操作。