Lesson 1 • Beginner
Introduction to PHP 🐘
By the end of this lesson you'll know what PHP is, how it runs on a web server, and how to write and output your first lines of PHP — the foundation of every dynamic page you'll ever build.
What You'll Learn in This Lesson
- Explain what PHP is and why it powers around 75% of the web
- Open and close PHP with the {'<'}?php ... ?> tags
- Output text to the page with echo and print
- Embed PHP inside an HTML file so a page becomes dynamic
- Write comments three ways: //, #, and /* ... */
- Run a PHP file with a web server or the php command line
php file.php or php -S localhost:8000. The Output panel under each example shows exactly what to expect.1️⃣ What is PHP?
PHP (it stands for "PHP: Hypertext Preprocessor") is a server-side scripting language — meaning the code runs on the web server, not in the visitor's browser. The server runs your PHP, the PHP produces HTML, and only that finished HTML is sent to the browser. This is the opposite of JavaScript, which runs in the browser. Created by Rasmus Lerdorf in 1995, PHP powers roughly 75% of all websites whose server language is known — including WordPress, Wikipedia, and apps built with frameworks like Laravel. Here is the very first thing every PHP programmer writes.
<?php
// Your very first PHP script. Everything PHP runs between <?php and ?>.
// 'echo' prints text to the page. Every statement ends with a semicolon ( ; ).
echo "Hello, World!\n";
// You can echo more than one line — each echo is its own statement.
echo "PHP is running on the server.\n";
// 'print' does almost the same job as echo (more on that below).
print "Goodbye for now!\n";
?>Hello, World!
PHP is running on the server.
Goodbye for now!Notice three things already: code lives between <?php and ?>, echo sends text to the output, and every statement ends with a semicolon. The \n inside the quotes is a newline, so each message lands on its own line.
2️⃣ The <?php Tags, echo & print
PHP only runs the code wrapped in tags. You open with <?php and close with ?>; everything between them is executed on the server. To produce output you have two near-identical tools: echo and print. Use echo almost all of the time — it's a touch faster and can print several values at once (echo "a", "b";). print behaves the same for everyday text but only takes one value, so it's rarely needed. The first script above already showed both in action.
3️⃣ Comments
A comment is a note for humans that PHP completely ignores — it never reaches the browser. Comments explain why code does something and let you switch a line off without deleting it. PHP gives you three styles: // and # both comment out the rest of a single line, and /* ... */ wraps a block across many lines.
<?php
// This is a single-line comment. PHP ignores everything after the // .
# This is ALSO a single-line comment — the hash style works too.
/*
This is a block comment.
It can span as many lines as you like —
handy for longer notes or temporarily switching off code.
*/
echo "Comments never appear in the output.\n"; // inline comment, also ignored
echo "Only the echoed text is sent to the browser.\n";
?>Comments never appear in the output.
Only the echoed text is sent to the browser.4️⃣ Embedding PHP in HTML
PHP's superpower is that it slots straight into an HTML page. Anything outside the <?php ... ?> tags is sent to the browser exactly as written; anything inside is run first, and its output is dropped into that spot. That's how a static page becomes dynamic — the $name below could just as easily come from a login or a database. (A $variable is a named box for a value — that's the next lesson; here just notice it.)
<!DOCTYPE html>
<html>
<body>
<h1>My Page</h1>
<!-- The web server runs the PHP below and DROPS its output right here. -->
<?php
// This block runs on the server before the page is sent.
$name = "Alice"; // a variable always starts with a dollar sign
echo "<p>Hello, $name!</p>"; // inside "double quotes" PHP expands $name
?>
<p>This plain HTML is sent as-is.</p>
</body>
</html><!DOCTYPE html>
<html>
<body>
<h1>My Page</h1>
<p>Hello, Alice!</p>
<p>This plain HTML is sent as-is.</p>
</body>
</html>The browser receives the Output panel above — pure HTML. It never sees the word echo or your variables. That's why PHP is safe for hiding business logic and database passwords: the source stays on the server.
5️⃣ How PHP Runs
PHP follows a simple loop: browser requests a page → web server runs the PHP → PHP returns HTML → browser displays it. There are two everyday ways to run a file. For a quick script, php hello.php runs it through the PHP command-line tool and prints the result in your terminal. To serve a real page, run php -S localhost:8000 in your project folder and open http://localhost:8000 — PHP's built-in web server executes your .php files. (Tools like XAMPP or Docker bundle PHP with the Apache server for bigger projects.)
Now you try. The script below is almost complete — fill in each ___ using the 👉 hint, then run it and check it against the Output panel.
<?php
// 🎯 YOUR TURN — fill in each blank marked ___ , then run it.
// Remember: text goes in "double quotes" and every line ends with a semicolon.
// 1) echo your name
echo ___; // 👉 e.g. "My name is Sam\n"
// 2) echo a second line that says what you're learning
echo ___; // 👉 e.g. "I am learning PHP\n"
// ✅ Expected output (example):
// My name is Sam
// I am learning PHP
?>My name is Sam
I am learning PHP___ blanks with text in "double quotes" (end each with \n), then run it. Your output should be two lines.One more. Here a line needs to be commented out so PHP skips it. Add a // where the hint points.
<?php
// 🎯 YOUR TURN — only ONE of these lines should appear in the output.
// Turn the FIRST echo into a comment so PHP skips it.
___ echo "This line should be hidden.\n"; // 👉 put a // at the start to comment it out
echo "This line should be visible.\n";
// ✅ Expected output:
// This line should be visible.
?>This line should be visible.___ with // so the first echo is ignored. Only the second line should appear.Common Errors (and the fix)
- Your PHP code shows up as plain text on the page — you forgot the opening
<?phptag (or saved the file as.html). Without the tag the server has no reason to run it. Wrap the code in<?php ... ?>and use a.phpextension. - "Parse error: syntax error, unexpected ..." — almost always a missing semicolon at the end of the previous line. Every PHP statement must end with
;. Check the line above the one the error points to. - Nothing prints even though there's no error — you used
returnwhere you meantecho.returnhands a value back inside a function; it does not display anything. To show text on the page, useecho. - "Undefined constant" or unexpected output around a name — you wrote a variable without its
$. In PHP every variable needs the dollar sign: it's$name, never justname.
Pro Tips
- 💡 Default to
echo. It's the standard way to output text in PHP — reach forprintonly in the rare case you need a return value. - 💡 If a file is pure PHP (no HTML after it), you can leave off the closing
?>tag — it avoids accidental blank lines being sent to the browser. - 💡 Comment the "why", not the "what". Good comments explain intent; the code already shows the steps.
📋 Quick Reference — PHP Basics
| Syntax | Example | What It Does |
|---|---|---|
| <?php ... ?> | <?php echo "Hi"; ?> | Open / close a PHP block |
| echo | echo "Hello"; | Output text to the page |
| print "Hello"; | Output one value (returns 1) | |
| ; | echo "Hi"; | Ends every statement |
| // # | // note | Single-line comment |
| /* ... */ | /* note */ | Block (multi-line) comment |
Frequently Asked Questions
Q: What is PHP actually used for?
PHP is a server-side scripting language: it runs on the web server and builds the HTML that gets sent to the browser. It powers roughly three-quarters of all websites whose server language is known, including WordPress, Wikipedia, and applications built with frameworks like Laravel and Symfony. Anything that needs to talk to a database, handle a login, or process a form is a natural fit for PHP.
Q: What is the difference between echo and print?
Both send text to the output. echo is slightly faster, can take several values separated by commas (echo "a", "b";), and returns nothing. print can only take one value and technically returns 1, so it can be used inside an expression. For everyday output most PHP code uses echo — print is rarely needed.
Q: Do I need the <?php tag in every file?
You need it wherever you want PHP to run. A file can be pure HTML with no PHP at all, or it can mix HTML and PHP by opening a <?php block, doing some work, and closing it with ?>. Anything outside the tags is sent to the browser untouched; only the code inside the tags is executed.
Q: How do I run a PHP file on my own computer?
Install PHP, then from a terminal run it one of two ways. For a quick script, php myfile.php runs it through the PHP command-line tool and prints the result. To serve a page like a real website, run php -S localhost:8000 in your project folder and open http://localhost:8000 in a browser — PHP's built-in server executes your .php files for you.
Mini-Challenge: About Me
No code is filled in this time — just a brief and an outline. Write it yourself, run it on onecompiler.com/php or your own machine, then check your result against the expected output in the comments. This is exactly the write-run-check loop you'll use on every real script.
<?php
// 🎯 MINI-CHALLENGE: A tiny "about me" script.
// No code is filled in — work from the steps below, then run it.
//
// 1. echo a line: "Name: <your name>"
// 2. echo a line: "Town: <your town>"
// 3. echo a line: "Learning: PHP"
// 4. Add a block comment ( /* ... */ ) above your code describing what it does.
// 5. Add one inline // comment on any line.
//
// Tip: end every echo with \n so each fact is on its own line.
//
// ✅ Expected output (example):
// Name: Ada
// Town: London
// Learning: PHP
// your code here
?>🎉 Lesson Complete!
- ✅ PHP is a server-side language — it runs on the server and sends HTML to the browser
- ✅ PHP code lives between
<?phpand?>, and every statement ends with; - ✅
echo(andprint) output text to the page - ✅ PHP embeds inside HTML — code inside the tags runs, everything else is sent as-is
- ✅ Comments come in three styles:
//,#, and/* ... */ - ✅ Run a file with
php file.phpor serve it withphp -S localhost:8000 - ✅ Next lesson: Variables & Data Types — store text, numbers, and true/false values in
$variables
Sign up for free to track which lessons you've completed and get learning reminders.