How to display PHP source code on the web page?

By Faruque Ahamed Mollick

Whenever you open a PHP file with your server path, it runs the PHP code. It will never display you the PHP source code. Whenever you put some PHP code inside that file, it will run the code instead of showing the source code on the web page.

But suppose you want to show the PHP source with highlighted and with the colors defined then how will you do that? How can you display the highlighted source code on web pages?

How to get the file size in PHP?

Protect website from SQL Injection in PHP

Here I am going to tell you how you can do it. I will show you how to display PHP source codes on your web page instead of running it.

Well, there are multiple ways available which can help you to do that. I am now going to tell you four simple and easy ways of displaying the highlighted version of PHP source code with the colors defined. Here are these:



  • Using PHP highlight_string() function
  • Using the show_source() PHP function
  • Using highlight_file() function
  • Use .phps file extension instead of .php

Now I am going to give you the example of doing it using each process that I have listed above.

Display highlighted PHP source code using PHP highlight_string() function

The PHP highlight_string() function outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.

Process form value in PHP using jQuery AJAX method

Below is the syntax of this function:

highlight_string(string, Return_Boolean)

The string parameter is the required parameter in the above syntax which specifies what to highlight. You have to pass the PHP code through the string.

Below is an example which shows the highlighted version of the given PHP code using the colors defined:

<?php
 highlight_string('
 	<?php 
 	   $var = "This is a string";
 	   echo $var;
         ?>
     ');
?>

Show PHP source code using highlight_file() function

highlight_file() is also a built in function of PHP which we can use to get the source code of a PHP file. here we just need to give the file path and it will return the complete highlight version of the source code of that PHP file.

PHP Weather Forecast Script That Uses OpenWeatherMap API

Simple PHP code for file uploading process

Suppose we want to get the source code of a PHP file filename.php, then we can get the source code of filename.php from the below code:

highlight_file('filename.php');

Display source code using the show_source() PHP function

The show_source() PHP  function is an alias of: highlight_file(). In this function also you have to pass the PHP file path like that you have just seen in the case of highlight_file() function. Below is the given code which uses show_source() function to get the highlight colored version of the source code of filename.php file:

show_source('filename.php');

Get source code using .phps extension

If you want to get the PHP source code directly by opening the URL in your browser then it will never possible as the server will run the codes and it returns the result. But if you use the extension .phps, then it will return you the complete colored and highlighted version of the PHP source code.

PHP urlencode And urldecode functions and it’s usage


I hope you have understood how to show PHP source code on a web page. All the four process I have discussed here are so simple and also understandable. If you like this post then please share it with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *