PHP Code Showing Instead of Running

Why does PHP code show as text instead of executing?

You've written your first PHP program, but when you go to run it, all you see in your browser is the code—the program doesn't actually run. When this happens, the most common cause is that you are trying to run PHP somewhere that doesn't support PHP.

Running PHP on a Web Server

If you are running PHP on a web server, make sure you have a host that is set up to run PHP. Although most web servers support PHP nowadays, if you aren't sure, a quick test can give you the answer.

In any text editor, create a new file and type:

 phpinfo() ; 
?>

Save the file as test.php and upload it to the root folder of your server. (Windows users make sure to display all file extensions.) Open a browser on your computer and enter the URL of your file in the format:


http://nameofyourserver/test.php

Click Enter. If the web server supports PHP, you should see a screen filled with information and a PHP logo at the top. If you don't see it, your server doesn't have PHP or PHP is not started properly. Email the web server to ask about your options.

Running PHP on a Windows Computer

If you are running your PHP script on a Windows computer, you need to manually install PHP. If you haven't already done so, your PHP code won't execute. Instructions for the installation process, versions and the system requirements are listed at the PHP website. After it is installed, your browser should run your PHP programs directly from your computer.

Running PHP on a Mac Computer

If you are on an Apple, you already have Apache and PHP on your computer. You just need to activate it to get things working. Activate Apache in the Terminal, which is located in the Utilities folder, by using the following command instructions.

Start Apache web sharing: 


sudo apachect1 start

Stop Apache web sharing:


sudo apachet1 stop

Find the Apache version:


httpd -v

In macOS Sierra, the Apache version is Apache 2.4.23.

After you start Apache, open a browser and enter:


http://localhost

This should display "It Works!" in the browser window. If not, troubleshoot Apache by running its config file in Terminal.


apachect1 configtest 

The configuration test may give some indications why the PHP isn't executing.