What is PHP?

Course 1 · Ch 1
What PHP Is — Embedding, Syntax, echo/print
A server-side language that lives right inside your HTML — what that actually means, and your first real PHP statements

Every course so far in this curriculum — HTML, CSS — runs entirely in the browser. PHP is different: it runs on the server, before the page ever reaches a visitor's browser at all. By the time a browser receives the response, every bit of PHP has already executed — the visitor never sees PHP code itself, only the HTML it produced.

PHP Tags — Embedding PHP Inside HTML

<!DOCTYPE html> <html> <body> <h1>Welcome</h1> <?php echo "<p>This paragraph was generated by PHP.</p>"; ?> </body> </html>

<?php opens a PHP block; ?> closes it. Everything outside these tags is passed through to the browser completely untouched, as plain HTML — everything inside is executed by the server first.

What the browser actually receives
<h1>Welcome</h1>
<p>This paragraph was generated by PHP.</p>
The .php file extension is what tells the server to actually run PHP at all
A file saved as .html containing PHP tags won't execute them — the server simply serves the literal <?php ... ?> text as-is, visible to anyone viewing the page source. The file must be saved with a .php extension for the server to recognise and run the PHP inside it.

echo and print — Outputting Content

<?php echo "Hello, world!"; print "This works too."; // echo can take multiple comma-separated values echo "Multiple", " ", "parts", " combined."; ?>

echo and print both output text — echo is marginally faster and can take several comma-separated values at once; print only ever takes one. In practice, echo is by far the more commonly used of the two.

String Interpolation — Mixing Variables into Output

<?php $name = "Philip"; echo "Hello, $name!"; // double quotes interpolate variables directly echo 'Hello, $name!'; // single quotes do NOT — outputs literally "$name" ?>
Double quotes vs single quotes is a real, practical distinction in PHP
Unlike many languages where quote style is purely stylistic, PHP treats double-quoted strings specially — variables inside them are automatically substituted with their value. Single-quoted strings are always taken completely literally. Both are valid; the choice matters for whether you actually want that substitution to happen.

Statements End with a Semicolon

<?php echo "First statement"; // the semicolon marks the end of this statement echo "Second statement"; ?>

Forgetting a semicolon is one of the most common beginner errors in PHP — the parser genuinely can't tell where one statement ends and the next begins without it, and the resulting error message often points at the following line rather than the actual missing semicolon.

Comments

<?php // A single-line comment # Also a single-line comment — less common, but valid /* A multi-line comment block */ ?>

Coding Challenges

Challenge 1

Create a PHP file that outputs a complete HTML page with the title "My First Page" and a single paragraph saying "PHP is running on the server!" — using echo for the paragraph specifically, with the rest as plain HTML outside the PHP tags.

📄 View solution
Challenge 2

Create a variable called $favouriteLanguage set to "PHP", then echo a sentence using double-quote interpolation that includes the variable's value. Then echo the exact same sentence using single quotes, and observe the difference in the output.

📄 View solution
Challenge 3

Write a PHP block with three separate echo statements that together output three lines (use "<br>" at the end of each string to force a line break in the browser): your name, your favourite number, and a one-sentence comment about why you're learning PHP.

📄 View solution

Chapter 1 Quick Reference

  • <?php ... ?> — opens/closes a block of PHP code embedded within HTML
  • .php file extension — required for the server to actually execute the PHP, not just serve it as text
  • echo / print — output content; echo is more common and takes multiple comma-separated values
  • Double quotes interpolate variables directly; single quotes never do
  • Every statement ends with a semicolon — a very common source of beginner errors when forgotten
  • // and # — single-line comments; /* */ — multi-line comments
  • Next chapter: variables, data types, and type juggling