highlight_file
Description
The highlight_file of Miscellaneous for PHP syntax highlighting of a file.
Syntax
highlight_file( string $filename, bool $return = false ): string|bool
Parameters
filename
Path to the PHP file to be highlighted.
return
Set this parameter to true to make this function return the highlighted code.
Return
If return is set to true, returns the highlighted code as a string instead of printing it out. Otherwise, it will return true on success, false on failure.
Examples
1 · filename
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/highlight_file.txt'; highlight_file($filename); ?>
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span></code></pre>
2 · return · false
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/highlight_file.txt'; $return = false; highlight_file($filename, $return); ?>
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span></code></pre>
3 · return · true
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/highlight_file.txt'; $return = true; highlight_file($filename, $return); ?>
4 · output · return · false
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/highlight_file.txt'; $return = false; $output = highlight_file($filename, $return); var_export($output); ?>
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span></code></pre>true
5 · output · return · true
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/highlight_file.txt'; $return = true; $output = highlight_file($filename, $return); echo $output; ?>
<pre><code style="color: #000000"><span style="color: #0000BB"><? </span><span style="color: #FF8000">// comment </span><span style="color: #007700">function </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">() { </span><span style="color: #0000BB">$myvariable </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">; echo </span><span style="color: #0000BB">$myvariable</span><span style="color: #007700">; } </span><span style="color: #0000BB">myfunction</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span></code></pre>
6 · ini_set
<? ini_set('highlight.comment', '#ffff00'); ini_set('highlight.default', '#0000ff'); ini_set('highlight.html', '#000000'); ini_set('highlight.keyword', '#00ff00'); ini_set('highlight.string', '#ff0000'); $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/highlight_file.txt'; highlight_file($filename); ?>
<pre><code style="color: #000000"><span style="color: #0000ff"><? </span><span style="color: #ffff00">// comment </span><span style="color: #00ff00">function </span><span style="color: #0000ff">myfunction</span><span style="color: #00ff00">() { </span><span style="color: #0000ff">$myvariable </span><span style="color: #00ff00">= </span><span style="color: #ff0000">"hello"</span><span style="color: #00ff00">; echo </span><span style="color: #0000ff">$myvariable</span><span style="color: #00ff00">; } </span><span style="color: #0000ff">myfunction</span><span style="color: #00ff00">(); </span><span style="color: #0000ff">?></span></code></pre>