Skip to content
Happy Programming Guide
Start learning
PHP

PHP Code Showing as Plain Text Instead of Running

Your PHP appears in the browser as source code, or the page is blank where the output should be. The file is not being processed by PHP. Five causes, in the order to check them.

A laptop with a cup of coffee beside it

When you open a page and see <?php echo "Hello"; ?> in the browser instead of Hello, or the page is blank where output should be, PHP never ran. The web server handed the file straight to the browser. That happens for five reasons, and the first two account for almost every beginner case: the file was opened directly from the disk rather than through a server, or it has the wrong extension.

Cause 1: you opened the file, not a URL#

Look at the address bar. If it starts with file:// or shows a path like C:/xampp/htdocs/test.php, the browser read the file from disk. No server was involved, so no PHP ran. The browser shows PHP source because it does not know what else to do with it.

PHP runs on a server, and the browser must ask the server for the page:

Output
http://localhost/test.php          # XAMPP, WAMP: file in htdocs or www
http://localhost:8888/test.php     # MAMP default port
http://localhost:8000/test.php     # PHP's built-in server, below

The quickest server of all is built into PHP itself. From the folder containing your file:

Terminal
php -S localhost:8000

Then open http://localhost:8000/test.php. If that works and XAMPP does not, the problem is XAMPP configuration, not your code.

Cause 2: the file is not .php#

A file named index.html is served as HTML no matter what is inside it. PHP inside it is shown as text, or, since browsers treat <?php as an unknown tag, silently hidden. Rename the file to .php. Editors that “helpfully” append .txt or keep .html are a common cause; check the real extension in the file manager with extensions shown.

Cause 3: the file is in the wrong folder#

Each local server stack serves one specific folder:

Stack Serve from URL
XAMPP C:\xampp\htdocs or /Applications/XAMPP/htdocs http://localhost/
WAMP C:\wamp64\www http://localhost/
MAMP /Applications/MAMP/htdocs http://localhost:8888/
Laragon C:\laragon\www http://localhost/ or http://project.test/

A file on the Desktop cannot be reached through localhost. Move it into the served folder, or point the built-in server at wherever it is.

Cause 4: the server is not running#

In the XAMPP or MAMP control panel, Apache must show as running, in green. If the browser says “This site can’t be reached” or “connection refused”, it is not. Start it. If it refuses to start, something else is using port 80 — on Windows, commonly Skype, IIS or the World Wide Web Publishing service. The control panel’s log names the port conflict.

Cause 5: short tags#

Code written as <? echo "hi"; ?> without the php uses short open tags, which are disabled on most installations. PHP treats the block as literal text and the browser hides it as an unknown tag, so the output simply disappears. Always write the full tag:

PHP
<?php echo "hi"; ?>
<?= $name ?>          <!-- this short echo form is always enabled -->

Apache is running but PHP is not attached#

If the URL is right, the file is .php, and the browser still downloads the file or shows the source, Apache is serving it without a PHP handler. This is common after a system upgrade on Linux, and occasionally on a fresh manual Apache install. Test the PHP interpreter directly:

Terminal
php -v                    # PHP itself works?
php test.php              # runs the file from the command line, ignoring the server

If both work, the fault is the Apache-to-PHP link. On Debian and Ubuntu that is the libapache2-mod-php package and a2enmod; on a manual install it is the LoadModule and AddType lines in httpd.conf. The XAMPP and MAMP bundles set this up for you, which is why they are the sensible starting point.

Blank page instead of source#

A completely blank page means PHP did run and then hit an error it is configured not to display. Add these two lines at the very top of the file, temporarily:

PHP
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

Reload, and the error appears with its line number. Remove the lines before deploying; errors belong in the log on a live site.

Questions people ask#

Can I run PHP without installing a server stack?

Yes. Install PHP alone and use php -S localhost:8000. For learning and small projects it is all you need.

Why does VS Code’s Live Server show the PHP as text?

Live Server serves static files only; it has no PHP. Use the built-in PHP server or a stack, and open the page from that.

Does the file need to be called index.php?

No. index.php is just what is served when the URL names a folder without a file. Any name ending in .php works when requested directly.

My PHP shows in the browser only on one page.

That page has the wrong extension or a short open tag. Compare it with a page that works.

Where to go next#

PHP errors not showing: display_errors and the error logRead next

Keep reading

PHP

PHP Security Best Practices

The security work that actually matters in PHP: prepared statements, output escaping, CSRF tokens, password hashing, file uploads and session handling.

6 min read

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

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