show_source() function is used to output the source code of a file and automatically calls highlight_file() to highlight the source code. In simple terms, show_source() is essentially a wrapper around the highlight_file() function, but it provides additional control options.
<span><span><span class="hljs-title function_ invoke__">show_source</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$return</span></span><span> = </span><span><span class="hljs-literal">false</span></span><span>): </span><span><span class="hljs-keyword">void</span></span><span>
</span></span>
$filename: The file path whose source code needs to be displayed.
$return: If set to true, the function returns the source code content instead of outputting it directly.
highlight_file() function is used to output a highlighted version of a file’s content. It highlights the PHP source code of a file so that developers can more easily read the source code. This function does not require any additional settings to highlight the file content.
<span><span><span class="hljs-title function_ invoke__">highlight_file</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$return</span></span><span> = </span><span><span class="hljs-literal">false</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-keyword">void</span></span><span>
</span></span>
$filename: The file path whose source code needs to be highlighted.
$return: If set to true, the function returns the highlighted version of the source code instead of directly outputting it.
show_source() is a wrapper around highlight_file(), so it provides similar functionality—highlighting PHP source code. The difference is that show_source() allows you to specify whether to return the source code, whereas highlight_file() does not offer this option.
highlight_file() is solely used for displaying highlighted source code, making it ideal for situations where code highlighting is needed. On the other hand, show_source() not only displays highlighted content but also provides the option to return the source code, making it more versatile in specific scenarios.
highlight_file() by default outputs the highlighted code in HTML format, meaning the outputted code can be directly displayed in the browser. It typically wraps the output in a This code will output the source code of the example.php file, with syntax highlighting applied. This code will also output the highlighted source code of example.php, but highlight_file() does not return content; it simply outputs it directly.show_source()</span>
3.2 Using the highlight_file() Function
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$filename</span></span><span> = </span><span><span class="hljs-string">"example.php"</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">highlight_file</span></span><span>(</span><span><span class="hljs-variable">$filename</span></span><span>);
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>