Question
I am trying to run PHP code in a project using Dreamweaver and XAMPP, but the PHP code is not being executed. Instead, it appears in the browser page source as if it were HTML.
Apache is running, the file has a .php extension, and the page opens in the browser. Why would PHP be displayed rather than executed, and how can I fix it?
<?
include_once("/code/configs.php");
?>
Short Answer
PHP runs on the web server before HTML is sent to the browser. If PHP source appears in the browser, the server is sending the file as plain text instead of passing it to PHP, or the code uses PHP syntax that is not enabled. You will learn how to verify that XAMPP is serving the file correctly, test PHP processing, and use the portable <?php opening tag.
Concept
PHP is a server-side language. The browser should never receive normal PHP source code.
When a browser requests a PHP file, the expected sequence is:
- The browser requests a URL such as
http://localhost/my-project/index.php. - Apache recognizes the
.phpfile extension. - Apache sends the file to the PHP interpreter.
- PHP executes the code and produces output, usually HTML.
- Apache sends only that output to the browser.
For example, the browser receives this:
<p>Hello, Ada!</p>
—not this:
<?php echo "Hello, Ada!"; ?>
If raw PHP appears in page source, it is an important configuration and security warning: PHP has not processed that content. This can expose application code, paths, credentials, and database configuration.
There are two especially relevant issues in the provided code:
- The file may be served by something other than Apache/PHP, such as opening it directly from disk or using an incorrect server location.
<?is a short PHP opening tag. It works only when the PHPshort_open_tagsetting is enabled. Use<?phpinstead because it is always supported.
Mental Model
Think of Apache as a restaurant waiter and PHP as the kitchen.
- A request for an HTML file is like ordering a bottled drink: Apache can hand it directly to the browser.
- A request for a PHP file is like ordering a cooked meal: Apache must send the file to PHP first, and PHP returns the finished result.
If the browser receives PHP code itself, Apache has handed over the recipe instead of sending it to the kitchen. Either PHP is not connected to Apache for that request, or PHP did not recognize the code as PHP.
The <?php tag is the clear label that tells PHP, “the instructions begin here.” The shorter <? label may not be accepted by the server configuration.
Syntax and Examples
Use the full PHP opening tag:
<?php
include_once __DIR__ . "/code/configs.php";
A basic PHP test file named test.php can contain:
<?php
echo "PHP is working";
When you visit it through XAMPP, for example:
http://localhost/my-project/test.php
the page should display:
PHP is working
The page source should contain only that text, not the <?php ... ?> code.
Prefer <?php over <?
<?
echo "May not run";
?>
The preceding code depends on the short_open_tag PHP configuration setting. It is not portable.
<?php
echo "Runs regardless of short_open_tag";
The full opening tag is the standard choice for PHP files.
Including a project file safely
<?php
include_once __DIR__ . "/code/configs.php";
__DIR__ means “the directory containing the current PHP file.” It helps make the include path independent of the server's current working directory.
Avoid assuming this is a project-relative path:
<?php
include_once "/code/configs.php";
A path beginning with / is usually an absolute filesystem path on the server, such as /code/configs.php, not automatically a path from your website's root.
Step by Step Execution
Consider this file:
<?php
$name = "Mina";
echo "Hello, " . $name;
When requested at http://localhost/example/index.php, this happens:
- The browser sends a request for
/example/index.php. - Apache finds
index.phpinside its configured web root, such as XAMPP'shtdocsfolder. - Apache passes the file contents to the PHP interpreter.
- PHP assigns
"Mina"to$name. - PHP joins
"Hello, "and$name. - PHP sends
Hello, Minaas output. - Apache returns that output to the browser.
The browser does not know that $name existed. It only receives:
Hello, Mina
If the browser instead receives the original <?php code, steps 3 through 6 did not occur for that request.
Real World Use Cases
PHP processing is needed whenever a page must generate a response on the server, including:
- Login pages: Verify a password and create a session before returning a response.
- Database pages: Query products, articles, or user records and render them as HTML or JSON.
- Configuration loading: Include environment-specific settings such as database host names.
- Form handling: Validate submitted data and save it to a database.
- APIs: Return JSON from PHP code, for example a list of orders.
- Access control: Check whether a user is authorized before serving private content.
In all of these cases, clients should receive the resulting HTML or JSON, never the PHP implementation or secret configuration values.
Real Codebase Usage
In projects, developers usually verify server-side PHP with a small health-check route or file during setup:
<?php
header("Content-Type: text/plain");
echo "PHP is available";
They also use full tags consistently:
<?php
// Application code
A common configuration-loading pattern is:
<?php
require_once __DIR__ . "/config/config.php";
Use require_once when the application cannot work without the file. PHP stops with an error if it is missing. Use include_once when the included file is optional or the program can continue without it.
For real applications, keep sensitive configuration files outside the public web directory when possible. Even with correct PHP setup, this reduces the risk of accidental exposure if the server is misconfigured.
When diagnosing a local server, developers check these items in order:
- The browser URL uses
http://localhost/..., not afile:///...path. - The project is inside Apache's configured document root or virtual-host directory.
- Apache and PHP are installed and connected in the active XAMPP configuration.
- A minimal
<?php echo "ok";file executes. - The PHP error log is checked for startup or configuration errors.
Common Mistakes
Opening the file directly from the filesystem
This bypasses Apache and PHP:
file:///C:/xampp/htdocs/my-project/index.php
Use a server URL instead:
http://localhost/my-project/index.php
Using the short opening tag
This may be treated as ordinary text when short_open_tag is disabled:
<?
echo "Hello";
?>
Use the portable full tag:
<?php
echo "Hello";
Assuming the .php extension alone guarantees execution
A .php extension is necessary in many setups, but Apache must also be configured to send .php files to PHP. A static server, a misconfigured virtual host, or a wrong Apache configuration can still send the file unchanged.
Testing a complex page first
An include, framework bootstrap, or template can make the issue harder to isolate. Start with:
<?php
echo "ok";
If this does not run, the problem is server setup, not your application code.
Using an incorrect include path
This path starts at the server filesystem root, not necessarily your project folder:
<?php
include_once "/code/configs.php";
For a code directory next to the current file, use:
<?php
include_once __DIR__ . "/code/configs.php";
Leaving exposed PHP source online
Do not ignore source code appearing in the response. Remove public access until PHP handling is fixed, especially if files contain passwords, API keys, or database credentials.
Comparisons
| Situation | What processes the file? | What the browser receives |
|---|---|---|
Open file:///.../page.php | No web server | Usually raw file contents or browser-dependent behavior |
Visit http://localhost/page.html | Apache serves a static file | HTML exactly as stored |
Visit http://localhost/page.php with PHP configured | Apache then PHP | PHP-generated output |
Visit .php through a server without PHP handling | Server serves it as text/static content | Raw PHP source, which is unsafe |
| PHP opening form | Recommended? | Notes |
|---|---|---|
<?php | Yes | Standard and always available in PHP. |
<?= | Yes | Short echo tag; equivalent to <?php echo. |
<? | No | Depends on short_open_tag; avoid it. |
| Include statement | Missing file behavior | Typical use |
|---|---|---|
include_once | Emits a warning and continues | Optional files or optional templates |
require_once | Stops with a fatal error | Essential configuration, functions, and bootstrapping |
Cheat Sheet
<?php
// Standard PHP opening tag
echo "Hello";
// Include a file relative to this file's directory
require_once __DIR__ . "/config/config.php";
- Request PHP through a web server URL:
http://localhost/... - Do not open PHP files using
file:///... - Use
<?php, not<? - Test PHP handling with
<?php echo "ok"; - Place local XAMPP projects in the configured Apache document root, commonly
xampp/htdocs - If PHP source is visible, treat it as a server configuration/security problem
- A leading
/in an include path commonly means an absolute server filesystem path - Check Apache and PHP error logs when a minimal test file fails
FAQ
Why does my browser show PHP code instead of running it?
The file was sent to the browser without being processed by PHP. Usually you opened it outside a server, used a server without PHP configured, or Apache is not passing .php files to PHP.
Does naming a file .php make it execute automatically?
No. The file must also be requested through a web server configured with PHP support, such as Apache in a correctly configured XAMPP installation.
Should I use <? or <?php in PHP files?
Use <?php. The shorter <? tag depends on the short_open_tag setting and may display as plain text.
How can I quickly test whether XAMPP executes PHP?
Create test.php in your project with <?php echo "PHP works"; and visit it at http://localhost/your-project/test.php.
Why does file:///C:/.../index.php not run PHP?
That URL tells the browser to read a local file directly. It does not send a request to Apache, so PHP has no chance to execute it.
Why might my included file still fail after PHP starts working?
The include path may be wrong. Use __DIR__ with a relative path, such as require_once __DIR__ . "/code/configs.php";.
Is showing raw PHP source dangerous?
Yes. PHP files can contain database passwords, API keys, business logic, and filesystem details. Fix the server configuration before deploying or sharing the site.
Mini Project
Description
Create a small PHP diagnostics page for a local XAMPP project. It confirms that the request is passing through PHP and demonstrates including a configuration file using a path based on the current script directory.
Goal
Display a successful PHP-processing message and a value loaded from an included configuration file.
Requirements
Create a project folder inside XAMPP's Apache document root, such as htdocs/php-checker.
Keep learning
Related questions
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.