What is PHP?
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
<?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.
<p>This paragraph was generated by PHP.</p>
.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
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
Statements End with a Semicolon
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
Coding Challenges
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.
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.
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.
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