Hello World in PHP with XAMPP

Get a real web server running on your own machine, understand why you cannot just double-click a .php file, and serve your first dynamic page.

PHP & the Web Beginner 10 min read Watch the video (3:35)

Every other language in this course runs your file directly. PHP is different, and understanding why saves you the most common frustration in web development.

#Why you cannot just open the file

Open an HTML file in your browser and it works. Open a PHP file the same way and you get… the raw source code, or a download prompt.

That is not a bug. PHP runs on the server, before the browser ever sees anything. The sequence is:

  1. Browser asks a server for index.php.
  2. The server notices the .php extension and hands the file to the PHP interpreter.
  3. PHP executes the code and produces HTML.
  4. The server sends that HTML — and only that HTML — to the browser.

The browser never sees a single line of PHP. That is why you can safely put a database password in a PHP file and why "View Source" never reveals your logic.

So to run PHP, you need a server. XAMPP is the easiest way to get one on your own machine.

#Step 1 — Install XAMPP

Download it from apachefriends.org for your OS. The name is an acronym: X (cross-platform), Apache, MariaDB, PHP, Perl. You only need the first three today.

Run the installer with the defaults. On Windows it lands in C:\xampp; on macOS, /Applications/XAMPP.

#Step 2 — Start Apache

Open the XAMPP Control Panel and click Start next to Apache. The label should turn green and show port numbers 80 and 443.

Now visit http://localhost in your browser. You should see the XAMPP dashboard. That page is being served by a real web server running on your own computer.

#Step 3 — Find htdocs

Apache only serves files from one specific folder, called the document root. In XAMPP that is:

  • Windows: C:\xampp\htdocs
  • macOS: /Applications/XAMPP/htdocs
  • Linux: /opt/lampp/htdocs

htdocs is http://localhost. A file at htdocs/hello.php is served at http://localhost/hello.php. A file on your Desktop is served nowhere at all.

Make a folder inside htdocs called learn-php. Everything in this course goes in there, reachable at http://localhost/learn-php/.

#Step 4 — Hello, world

Create htdocs/learn-php/index.php:

php
<?php
echo "Hello, world!";
?>

Visit http://localhost/learn-php/. There it is.

Some notes on those three lines:

  • <?php opens PHP mode. Everything after it is code until ?> closes it.
  • echo outputs text. It is not a function, so it needs no parentheses.
  • Statements end with a semicolon. PHP is strict about this in a way Python is not.

#Step 5 — PHP mixed into HTML

The real power is interleaving. PHP is a template language at heart:

php
<?php
$name = "Dom";
$lessons = 5;
$today = date("l");
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Learn PHP with <?php echo $name; ?></title>
</head>
<body>
  <h1>Hello from <?php echo $name; ?>!</h1>
  <p>Happy <?php echo $today; ?>. There are <?php echo $lessons; ?> lessons in this course.</p>

  <ul>
    <?php for ($i = 1; $i <= $lessons; $i++): ?>
      <li>Lesson <?php echo $i; ?></li>
    <?php endfor; ?>
  </ul>
</body>
</html>

Load that and view the page source in your browser. You will see clean HTML with a real list of five items — no PHP anywhere. The loop ran on the server and vanished.

Two pieces of syntax worth knowing:

<?= ?> is shorthand for <?php echo ?>. These are identical:

php
<h1>Hello from <?php echo $name; ?>!</h1>
<h1>Hello from <?= $name ?>!</h1>

Use the short one in templates. It is enabled by default in modern PHP and it makes markup far easier to read.

The colon-and-endfor style (for (...):endfor;) exists specifically for templates. When HTML sits between the braces, endfor is much easier to match up by eye than a lonely } twenty lines down. if/endif, foreach/endforeach and while/endwhile all work the same way.

#Variables and the basics

php
<?php
$title = "Coding with Dom";     // string — variables always start with $
$views = 5063;                  // int
$rating = 4.8;                  // float
$isFree = true;                 // bool

// Concatenation uses a dot, not a plus
echo $title . " has " . $views . " views";

// Or interpolate directly — double quotes only
echo "$title has $views views";

// Braces when the variable touches other characters
echo "That is {$views}x more than yesterday";

#Step 6 — When something breaks, see the error

By default XAMPP shows PHP errors, but if you get a blank white page instead of a message, turn reporting on explicitly while you are learning. Add this at the top of your file:

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

A blank page means PHP hit a fatal error and was told not to mention it. Never leave this on for a real public site — error messages leak file paths and internal structure to attackers — but during development, silence is your enemy.

Also worth knowing: Apache's error log lives at xampp/apache/logs/error.log. When even the error message is missing, the log has it.

Check yourself

4 questions on what you just read. No score is kept — it is only here to catch the bits that did not stick.

  1. 1You double-click a .php file and see the source code. Why?

    Show the answer

    A. PHP runs on a server; opening the file directly skips the interpreter entirely.

    The browser never sees PHP. Apache hands the file to the interpreter, which produces HTML, and only that HTML is sent. Serve it from htdocs and it works.

  2. 2Apache refuses to start and the log mentions port 80. Most likely cause?

    Show the answer

    A. Another program — Skype, IIS, another dev server — already holds the port.

    Either quit the other program or change Listen 80 to Listen 8080 in httpd.conf, then use http://localhost:8080.

  3. 3What does echo '$title' print when $title = "Coding with Dom"?

    Show the answer

    A. The literal text $title.

    Double quotes interpolate variables; single quotes do not. This catches everyone at least once.

  4. 4Why do professional PHP files often omit the closing ?>?

    Show the answer

    A. Any stray whitespace after it is sent to the browser and breaks headers and redirects.

    That trailing newline is the classic cause of "headers already sent". In a pure-PHP file, just end after the last statement.

#Key takeaways

  • PHP runs on the server; the browser only ever receives the resulting HTML.
  • Files must live in htdocs to be served. htdocs is http://localhost.
  • Port 80 conflicts are the usual cause of Apache refusing to start — change to 8080.
  • Use <?= ?> in templates and the endfor/endforeach style when HTML is involved.
  • Double quotes interpolate variables; single quotes do not.
  • Turn on display_errors while learning. A white page is a hidden error.

Next: making the page respond to what a user types.

Finished this one?

The challenge above has no posted solution — that is deliberate. Get it working, then come back for the next lesson.

Next lesson Subscribe