Why Don't I See My PHP Code When I View Source?

Why saving a PHP page from a browser doesn't work

Web developers and others who are knowledgeable about web pages know you can use a browser to view the HTML source code of a website. However, if the website contains PHP code, that code is not visible, because all the PHP code is executed on the server before the website is sent to a browser. All the browser ever receives is the result of the PHP embedded in the HTML. For this same reason, you cannot go to a .php file on the web, save it, and expect to see how it works.

You are only saving the page produced by the PHP, and not the PHP itself.

PHP is a server-side programming language, meaning it is executed at the web server before the website is sent to the end-user. This is why you can't see the PHP code when you view the source code.

Sample PHP Script

 <?php
 Echo "My PHP Page";
 ?> 

When this script appears in the coding of a web page or .php file that is downloaded by an individual to a computer, that viewer sees:

My PHP Page

Because the rest of the code is just instructions for the web server, it isn't viewable. A view source or a save simply displays the results of the code—in this example, the text My PHP Page.

Server-Side Scripting vs. Client-Side Scripting

PHP isn't the only code that involves server-side scripting, and server-side scripting isn't limited to websites. Other server-side programming languages include C#, Python, Ruby, C++ and Java. 

Client-side scripting operates with embedded scripts—JavaScript is the most common—that are sent from the web server to a user's computer.

All the client-side script processing takes place in a web browser on the end-user's computer.