📖 What is a PHP File?
A PHP file is a plain-text file containing source code written in PHP, a server-side scripting language built for the web. It uses the .php extension. Inside, PHP code sits alongside ordinary HTML, and when a web server runs the file, the PHP parts execute and produce HTML that the server sends to the browser. The visitor sees a finished web page and never the code behind it.
PHP stands for PHP: Hypertext Preprocessor, a recursive name. It was originally called Personal Home Page when Rasmus Lerdorf created it in 1995. Since then it has grown into one of the most widely used languages on the web. It runs around 70 percent of all websites whose server-side language is known, including WordPress, which alone powers over 40 percent of all sites.
The key idea is that a PHP file is processed before anyone sees the result. The server reads the file, runs the logic, pulls in data from a database if needed, and builds a plain HTML page. That is why PHP is called a preprocessor, and why opening a .php file directly rarely shows what you expect.
⚡ Quick Facts
| Full name | PHP: Hypertext Preprocessor |
| Extension | .php |
| MIME type (server) | application/x-httpd-php |
| MIME type (source) | text/x-php |
| File type | Server-side script, plain text |
| Created by | Rasmus Lerdorf |
| First appeared | 1995 |
| Latest version | PHP 8.5 |
| Maintained by | The PHP Group and PHP Foundation |
| Runs on | A web server such as Apache or Nginx |
⚙️ How a PHP File Runs
A PHP file never runs in the browser. It runs on the server first, and only its output travels to the visitor. This diagram shows the round trip.
PHP code lives between <?php and ?> tags. Everything outside those tags is sent as-is, and everything inside is executed. When this file runs on a server, the visitor receives a page with the heading "Hello, world!", not the code that produced it.
💻 How to Open a PHP File
Double-clicking a .php file shows raw code or triggers a download instead of a web page. That is expected. A browser on its own cannot run PHP. To see the finished page you need a web server, which is covered in the next section. To read or edit the code, a text or code editor is all you need.
Read or edit the code
A PHP file is plain text, so it opens in any editor, from Notepad or TextEdit up to a full development tool. A programmer editor such as Visual Studio Code, Sublime Text or Notepad++ is the practical choice, since it colour-codes the mixed PHP, HTML and SQL inside and warns about mistakes while you write. Steer clear of Microsoft Word and other word processors, whose hidden formatting corrupts source code. Phone users can install a code editor app to read the same source.
See it as a web page
To view the page a PHP file produces, it has to be processed by a server. On a live website that happens automatically. To do it on your own computer, install a local server package, then open the file through the server address rather than by double-clicking. The running steps are below.
▶️ How to Run a PHP File Locally
To run a PHP file on your own machine, you need a local web server with PHP installed. Packages like XAMPP, WAMP or MAMP bundle a server, PHP and a database together, so setup is a single install.
1. Install a server
Download XAMPP, WAMP or MAMP and install it. Each includes the Apache web server, the PHP engine and a database, ready to use.
2. Place the file
Copy your .php file into the server's web folder. In XAMPP this folder is called htdocs. The server can only run files kept there.
3. Open through the server
Start the server, then open a browser and go to the local address, such as http://localhost/yourfile.php. The page now runs and displays.
The rule to remember is that the address bar must start with http://localhost, not file://. Opening the file straight from a folder skips the server, so you see code again rather than a page.
📍 Where PHP Files Are Used
PHP runs the logic behind a large part of the web. If a site remembers who you are, processes a form or pulls content from a database, a PHP file is often doing that work.
Content management
WordPress, the most widely used website platform, is built in PHP. Its themes and plugins are PHP files, which is why editing a site often means opening one.
Web applications
Online stores, forums, booking systems and dashboards use PHP to handle accounts, process forms and generate pages on demand. Frameworks such as Laravel and Symfony give large applications a tested structure.
Database-driven pages
PHP frequently works with a MySQL database, reading and writing records so that a page shows current data every time it loads.
APIs and backends
A PHP file can also return data rather than a page, sending JSON responses that power a mobile app or a JavaScript front end.
⚔️ PHP vs HTML
PHP and HTML often live in the same file, but they do different jobs. Understanding the split explains why a PHP file behaves the way it does.
| Aspect | PHP | HTML |
|---|---|---|
| Runs where | On the server, before the page is sent | In the browser, on the visitor's device |
| Purpose | Logic, data and building the page | Structure and content of the page |
| Visible to visitor | No, only its output is sent | Yes, it is the page source |
| Needs a server | Yes, to execute | No, opens in any browser |
In short, HTML describes what a page looks like, and PHP decides what that HTML should be each time the page loads. A PHP file usually produces HTML, so the two are partners rather than rivals.
🔀 Related PHP Extensions
Alongside .php, a few related extensions appear in PHP projects. They mostly signal how a file is meant to be handled.
.phtml | An older extension for files that mix PHP and HTML. Some templating setups still use it. |
.php3 / .php4 / .php5 | Version-specific extensions from earlier eras. They are legacy and no longer recommended, since plain .php is standard now. |
.phps | A source view. When a server is set up for it, a .phps file displays the PHP code with colour highlighting instead of running it. |
.phar | A PHP Archive that bundles a whole application into a single file that PHP can run directly, similar to a Java JAR. |
⚠️ Common PHP File Problems
Most trouble with a .php file comes from the same few causes. These are the usual ones and what they mean.
The file opened without a server, or the server is not set up to run PHP. Open it through http://localhost on a running server rather than from a folder.
The server sent the file as a download instead of running it, usually because PHP is not enabled for that server or address.
The script ran but hit an error that is hidden. Turning on error display in the PHP settings, or checking the server log, reveals the cause.
A missing semicolon or unclosed bracket stops the script. A code editor with highlighting points to the line.
❓ Frequently Asked Questions
It depends what you want. To read or edit the code, open it in a code editor like Visual Studio Code or Notepad++, since the file is plain text. To see the web page it produces, the file must run on a web server. On a live site that is automatic, and on your own computer you install a local server like XAMPP and open the file through http://localhost.
Because the PHP was never run. A browser can display HTML, but it cannot execute PHP on its own. The code has to be processed by a web server first, which turns it into HTML. If you double-click a .php file or open it with file:// in the address bar, you skip the server and see the raw source. Open it through a running server at http://localhost instead.
PHP stands for PHP: Hypertext Preprocessor, a recursive acronym where the first word is the acronym itself. It was originally named Personal Home Page when Rasmus Lerdorf created it in 1995. The name changed as the language grew beyond its first purpose into a general tool for building web pages.
Install a local server package. XAMPP works on Windows, Mac and Linux, and MAMP is popular on Mac. After installing, copy your .php file into the server's web folder, called htdocs in XAMPP, start the server, then open a browser and go to http://localhost/yourfile.php. The file runs and the finished page appears. Opening the file directly from a folder will not run it.
Opening a .php file in a text editor to read it is safe, since viewing the code cannot run it. The caution is running one. A PHP file contains executable logic, so running a file from an unknown source on a server could do harm. If someone sent you a .php file you did not expect, read it in an editor first and do not place it on a live server until you trust it.
HTML runs in the browser and describes the structure of a page. PHP runs on the server and decides what that page should contain before it is sent. A visitor sees HTML but never the PHP, because PHP produces the HTML and then disappears. The two often sit in the same file, with PHP filling in the parts that change and HTML holding the parts that stay the same.
Not by renaming it, because the PHP logic has to run to produce HTML. The way to get the HTML is to run the file on a server, open the resulting page in a browser, then save that page. What you save is the HTML the PHP generated for that visit. The original .php file still holds the logic, which a static HTML copy does not keep.
A web server set up to run PHP uses application/x-httpd-php. When the raw source is served as text, the type is text/x-php. These tell a server how to handle the file. If PHP is misconfigured, a server may send a .php file as plain text, which is one reason the code appears in the browser instead of a page.
No, not from your browser. When a page runs, the server executes the PHP and sends only the resulting HTML, so View Source shows that HTML and never the PHP behind it. This is by design and keeps logic, passwords and database details private. You can only read a site's PHP if you have access to the server files, for example as the site owner or developer.
No. Opening, reading and running a plain .php file needs nothing beyond a code editor and, to run it, a local server. Frameworks such as Laravel and Symfony are tools that organise large PHP applications, and a file from one is still an ordinary PHP file you open the same way. You only need to learn a framework if you plan to build with it, not to view a file.
📝 Summary
A PHP file is a plain-text file of server-side code written in PHP, using the .php extension. A web server runs the file, executes the PHP between its tags, often reads a database, and returns finished HTML, so the visitor sees a page and never the code. Created by Rasmus Lerdorf in 1995 and now on version 8.5, PHP powers a large part of the web, including WordPress. To read a PHP file, open it in a code editor. To see the page it makes, run it through a local server such as XAMPP at http://localhost.