show_source() function in PHP

The show_source() function in PHP displays the source code of a PHP file with syntax highlighting. This function is useful for code documentation, debugging, or educational purposes where you need to display formatted PHP source code.

Syntax

show_source(file, return)

Parameters

  • file − The file name to display. This can be a relative or absolute path to the PHP file.

  • return − Optional boolean parameter. If set to true, the function returns the highlighted code as a string instead of printing it. Default is false.

Return Value

If the return parameter is true, it returns the syntax-highlighted code as a string. Otherwise, it outputs the highlighted code directly and returns true on success or false on failure.

Example 1: Displaying File Source

Here's how to use show_source() to display a PHP file with syntax highlighting ?

<?php
    // Display source code of current file
    show_source(__FILE__);
?>

Example 2: Returning Highlighted Code

Using the return parameter to get highlighted code as a string ?

<?php
    // Get highlighted code as string
    $highlighted = show_source("example.php", true);
    
    // Now you can manipulate or store the highlighted code
    echo "Code length: " . strlen($highlighted) . " characters";
    echo $highlighted;
?>

Key Points

  • The function requires the target file to exist and be readable
  • Output includes HTML tags with CSS classes for syntax highlighting
  • Commonly used with highlight_file() which is an alias for this function
  • The highlighted output uses predefined color schemes for different PHP syntax elements

Conclusion

The show_source() function is valuable for displaying PHP code with proper syntax highlighting. Use the return parameter when you need to process the highlighted output further rather than displaying it directly.

Updated on: 2026-03-15T07:37:36+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements