PHP BasicsCore· 30 min read

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 .php files in the htdocs folder.
  • 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:

  1. You type a web address like http://localhost/hello.php into the browser.
  2. The browser sends that request to the server running on your computer.
  3. The server sees the .php ending and hands the file to PHP to run.
  4. PHP runs the code top to bottom and produces plain HTML as the result.
  5. 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:

Terminal: PHP’s built-in dev server
# in the folder with your PHP files
php -S localhost:8000
# then open http://localhost:8000 in your browser

Note: 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:

hello.php
<?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?

Answer: PHP needs a server to process it and produce HTML. Open it via http://localhost, not by double-clicking the file.

✍️ Practice

  1. Install XAMPP (or use php -S) and run a hello.php that echoes a message.
  2. Confirm it works by opening it via http://localhost.

🏠 Homework

  1. Create a PHP page that outputs a small HTML structure (heading + paragraph) using echo.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →