LearnContact
Lesson 618 min read

Your First PHP Program

Write, save, and view your very first PHP script, mix PHP with HTML, and learn how to spot and fix the most common beginner errors.

Introduction

With PHP installed and a local server ready to go, it is time to write actual code. There is no better way to confirm your whole setup works end to end than writing a tiny script, saving it in the right place, and seeing it render in a browser.

This lesson walks through creating a classic "Hello, World!" style PHP file, viewing it through localhost, mixing PHP with plain HTML, and — just as importantly — recognizing the handful of mistakes almost every beginner makes on their first few PHP files.

What You Will Learn
  • How to write a basic hello.php file using <?php ?> tags and echo.
  • Where to save it and how to view it through http://localhost/.
  • How to mix PHP code with plain HTML in the same file.
  • How to write a small variation that does basic math and prints multiple values.
  • How to recognize and fix common first-run errors.

Writing hello.php

Every block of PHP code lives between <?php and ?> tags. Inside those tags, echo is the most basic way to output text. Here is the simplest possible PHP program.

hello.php
<?php
echo "Hello, World!";
?>

Save this file as hello.php inside your XAMPP htdocs folder (for example, C:\xampp\htdocs\hello.php). The closing ?> tag is technically optional at the end of a file, and many style guides recommend omitting it to avoid accidental whitespace issues — but including it, as above, is perfectly valid too.

Viewing It in the Browser

With Apache running from the XAMPP Control Panel (covered in the previous lesson), open a browser and navigate to your file's URL.

Browser address bar
http://localhost/hello.php
Rendered in the Browser
Hello, World!
What Just Happened

Apache received your browser's request for hello.php, handed it to the PHP interpreter, the interpreter executed the echo statement, and the resulting text was sent back as the page content — exactly the request-to-response flow covered in earlier lessons.

Mixing PHP with HTML

One of PHP's defining features is how naturally it mixes with plain HTML. You can freely switch in and out of <?php ?> tags anywhere inside an HTML document.

greeting.php
<!DOCTYPE html>
<html>
<head>
    <title>My First PHP Page</title>
</head>
<body>
    <h1>Welcome to My Site</h1>
    <?php
        $name = "Amol";
        echo "<p>Hello, $name! Thanks for visiting.</p>";
    ?>
</body>
</html>
Rendered in the Browser
Welcome to My Site
Hello, Amol! Thanks for visiting.

Example: Basic Math and Output

Here is a small variation that performs basic arithmetic and prints several values, showing how echo can be used more than once in a single script.

math.php
<?php
$price = 250;
$quantity = 3;
$total = $price * $quantity;

echo "Price per item: $price\n";
echo "Quantity: $quantity\n";
echo "Total cost: $total\n";
Rendered in the Browser
Price per item: 250
Quantity: 3
Total cost: 750
Note on Line Breaks

The \n characters in these examples create line breaks in the raw output. When viewed in a browser as HTML, line breaks like these are usually collapsed visually unless you use an HTML tag like <br> instead — something later lessons on output and HTML integration will cover in more depth.

Common First-Run Errors

Almost everyone hits at least one of these errors on their first few PHP scripts. Recognizing them quickly will save a lot of confusion.

Errors to Watch For
  • Forgetting the closing semicolon after a statement — PHP will throw a parse error pointing near the next line.
  • Seeing a completely blank white page — this usually means a fatal error occurred but display_errors is turned off, hiding the message.
  • Mismatched quotes, such as starting a string with a double quote and closing it with a single quote, which breaks the string and often the rest of the file.
  • Saving the file with the wrong extension (e.g. .txt instead of .php), so Apache serves it as plain text instead of running it through PHP.

If you ever get a blank page with no error message, temporarily enable error display at the top of your script while developing, so PHP tells you exactly what went wrong instead of failing silently.

debug-example.php
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);

echo "Now errors will show up on screen instead of a blank page.";
Rendered in the Browser
Now errors will show up on screen instead of a blank page.

Best Practices

  • Always save PHP files with a .php extension so the web server routes them to the PHP interpreter.
  • Enable display_errors while developing locally, and turn it off in production.
  • Double-check that every statement ends with a semicolon, especially when you are still building the habit.
  • Keep quote characters consistent and matched — pick either single or double quotes per string and close with the same type.

Frequently Asked Questions

Why does my page show completely blank instead of an error?

This typically happens when a fatal error occurs but display_errors is disabled (common in default configurations). Enable display_errors and error_reporting(E_ALL) temporarily to see the actual error message.

Do I need the closing ?> tag at the end of a PHP-only file?

No, it is optional, and many developers omit it in pure-PHP files to avoid accidental extra whitespace being sent as output after the tag.

Can I write plain HTML and PHP in the same file?

Yes — this is one of PHP's core features. You can freely switch between HTML and <?php ?> blocks anywhere in the same file.

What happens if I forget a semicolon?

PHP will usually throw a parse error, often pointing to the line after the missing semicolon, since it expected the statement to continue.

Why does my browser show my PHP source code instead of running it?

This usually means the file was not served through PHP at all — for example, it was opened directly from disk rather than through http://localhost/, or the file extension was not .php.

Key Takeaways

  • A basic PHP script uses <?php ?> tags with echo to output text.
  • Files saved in htdocs are viewed through http://localhost/filename.php.
  • PHP can be freely mixed with plain HTML in the same file.
  • A blank white page usually means a hidden fatal error — enable display_errors to see it.
  • Forgotten semicolons and mismatched quotes are the most common first-run mistakes.

Summary

Writing your first PHP program ties together everything from the previous lessons: PHP itself, a running web server, and the htdocs folder all come together the moment your hello.php page renders in the browser.

In this lesson, you wrote and viewed your first PHP file, mixed PHP with HTML, tried a small math example, and learned how to recognize the most common beginner errors. Next, you will take a closer, more formal look at PHP's syntax rules.

Lesson 6 Completed
  • You wrote and ran your first PHP script.
  • You can view a PHP file through http://localhost/.
  • You can mix PHP code with plain HTML markup.
  • You know how to spot and fix common first-run errors like missing semicolons and blank pages.
Next Lesson →

PHP Syntax