Installing & Running PHP
Set up a local server so your .php files actually run on your computer.
What you will learn
- Install a local PHP server (XAMPP)
- Create and run a .php file
- Understand why you cannot just double-click a .php file
You need a server
Unlike HTML, a .php file must be processed by a PHP server — double-clicking it just shows the code. The easiest setup is XAMPP (or the built-in PHP server).
- Install XAMPP (includes PHP, Apache and MySQL) from apachefriends.org.
- Put your
.phpfiles in thehtdocsfolder. - Start Apache, then open
http://localhost/yourfile.php.
Here is the whole picture in order — what happens from the moment you ask for a PHP page to the moment you see it:
- You type a web address like
http://localhost/hello.phpinto the browser. - The browser sends that request to the server running on your computer.
- The server sees the
.phpending and hands the file to PHP to run. - PHP runs the code top to bottom and produces plain HTML as the result.
- The server sends that finished HTML back to the browser, which shows it.
Or use the built-in server
PHP also has a tiny built-in web server — handy for learning, no XAMPP needed. Open a terminal in the folder that holds your PHP files and run:
# in the folder with your PHP files
php -S localhost:8000
# then open http://localhost:8000 in your browserNote: The line php -S localhost:8000 starts a small web server. -S means "serve", and localhost:8000 is the address it listens on. Leave that terminal window open while you work — closing it stops the server. Then visit http://localhost:8000 in your browser to see your pages.
Now make your first PHP page. Create a file called hello.php with this inside:
<?php
echo "Hello from PHP!";
?>Note: Output (in the browser, at http://localhost:8000/hello.php):
Hello from PHP!
The <?php ... ?> tags tell the server "this part is PHP, run it". echo means "send this text to the page". Notice the browser shows only the words Hello from PHP! — never the code itself, because PHP ran on the server first.
Watch out: Opening a .php file directly (file://) will not run it — you will see the raw code. Always open it through http://localhost... so the server processes it.
Q. Why can’t you just double-click a .php file to run it?
✍️ Practice
- Install XAMPP (or use
php -S) and run ahello.phpthat echoes a message. - Confirm it works by opening it via
http://localhost.
🏠 Homework
- Create a PHP page that outputs a small HTML structure (heading + paragraph) using
echo.