Skip to content

Web Nerdism w/ PHP 101 › Before You Start

Reading the Examples

Phillip Harrington Nov 2026

Listen, I'm not gonna lie to you: there's gonna be some code. This page is a key to the code in the early lessons. The first sections of the course are about the web, not PHP, but every lesson has a Try It, and every Try It has a little PHP in it. PHP itself starts properly in PHP Basics.

So here's every piece you'll see in the early examples, what it does in a sentence, and where it's explained for real. Some of it is going to look like line noise. Stay with me. You don't need to memorize any of it, and by the end of the page you'll be able to read a whole example top to bottom. Come back whenever a line looks strange.

What's in an Example?

The Opening Tag

A PHP file usually starts with <?php. Everything after it is PHP. In a file that mixes PHP with HTML, ?> switches back to HTML, and <?= $name ?> is a short way to print one value in the middle of it. Properly: PHP Files, Escaping into PHP, and phpinfo().

Variables

A $ and a name: $name, $count. A single = puts a value in it: $count = 3;. PHP has a lot of dollar signs. That's how some developers afford the Lambo. Properly: Variables, Types and strict_types.

The Semicolon

Every statement ends with ;, the way a sentence ends with a period. Lines that open a block with { don't get one. Properly: PHP Files.

echo

Prints. echo 'Hello'; sends Hello to the browser, or to the terminal. Commas print several things in a row: echo 'Hi, ', $name;. Properly: Output.

Single and Double Quotes

Both make a string, a piece of text. Double quotes fill in variables and understand \n, a new line: "Hello, $name\n" prints Hello, Alice and ends the line. Single quotes print exactly what's between them, dollar sign and all. Properly: Strings.

The Dot

Joins strings: 'Hello, ' . $name. Not +, which is only for numbers. Properly: Operators.

Casting: (int)

A type in parentheses in front of a value converts it: (int) '42' is the number 42, and (int) 'banana' is 0. The examples cast input to the type they expect, so a stranger's nonsense turns into something harmless. Properly: Variables, Types and strict_types.

Arrays and =>

A list in square brackets: ['small', 'medium', 'large']. Counting starts at 0, so $sizes[0] is small. An array can have names instead of numbers, written with =>: ['size' => 'large'], and then $shirt['size'] is large. $errors[] = 'Oops'; adds one to the end. Properly: Arrays.

??

"This, or if it's missing, that." $_GET['page'] ?? 1 is the page from the URL, or 1 if there isn't one. The examples use it everywhere, because input is so often missing. Properly: Operators.

Comparing and Deciding

=== asks "exactly equal?", and !== asks the opposite. && is and, || is or, ! is not. $wrap ? 'yes' : 'no' picks one of two values. Properly: Operators.

Calling a Function

A name, then parentheses with what you give it: strtoupper($name), count($sizes). PHP comes with thousands of them, and php.net explains every one. Properly: Functions, and Documentation: php.net.

if and foreach

if (test) { ... } runs the block in braces only when the test is true. foreach ($sizes as $size) { ... } runs it once for each item. In files full of HTML you'll see them written with a colon instead: if (...): through endif;, and foreach (...): through endforeach;. Same thing, easier to spot between the tags.

Comments

Notes for people, skipped by PHP: // or # to the end of the line, and /* ... */ around as many lines as you like. Properly: PHP Files.

What Came In

Names in capitals with an underscore, like $_GET, $_POST, $_COOKIE and $_SERVER, are arrays PHP fills in from the request: the query string, the form, the cookies, and everything else it knows. They all come from outside, so the examples check them before using them. Properly: Query Strings, Forms, and Never Trust Input.

How It Works

When PHP runs a file:

  1. It starts at the top.
  2. Anything outside the PHP tags, <?php and ?>, like HTML, is sent along exactly as it is.
  3. Anything inside <?php ... ?> runs, one statement at a time, top to bottom, except where an if skips a block or a foreach repeats one.
  4. Whatever it prints becomes the answer: the page in your browser, or the text in your terminal.

So you can read an example the same way: from the top, and anything it echoes is what you'll see.

Try It

This is the part where it clicks. Every piece above, in one file, with a comment over each line. In a folder, make cheat-sheet.php:

<?php
// Two slashes start a comment: a note for people. PHP skips it.
# A hash does the same.
/* And this kind can run
   over several lines. */

// A variable is a $ and a name. = puts a value in it.
// Every statement ends with a semicolon.
$name = 'Alice';
$count = 3;

// echo prints. "\n" starts a new line.
echo 'Hello', "\n";

// Double quotes fill in variables. Single quotes don't.
echo "Hello, $name\n";
echo 'Hello, $name', "\n";

// A dot joins strings together.
echo 'Hello, ' . $name . "\n";

// An array is a list, in square brackets. Counting starts at 0.
$sizes = ['small', 'medium', 'large'];
echo $sizes[0], "\n";

// Or a list of keys and values, with =>.
$shirt = ['size' => 'large', 'color' => 'blue'];
echo $shirt['color'], "\n";

// ?? means: this, or if it's missing, that.
echo $shirt['price'] ?? 'no price', "\n";

// Calling a function: its name, then what you give it, in ( ).
echo strtoupper($name), "\n";
echo count($sizes), "\n";

// if runs the block in { } only when the test is true.
if ($count > 2) {
    echo "More than two\n";
}

// foreach runs the block once for each item.
foreach ($sizes as $size) {
    echo "- $size\n";
}

reading-the-examples/cheat-sheet.php on GitHub

Run it with PHP, in a container:

docker run --rm -v "$PWD":/app -w /app php:8.4-cli php cheat-sheet.php

New here:

php:8.4-cli
The image: PHP, version 8.4, the command-line build. It's the one every lesson uses.
php
The command to run inside the container: PHP itself.
cheat-sheet.php
The file for PHP to run. Since /app is your folder, it finds the one you just made.

Match each line of output to the line that printed it. Then change things: a different name, a fourth size, a price in the shirt. Run it again after each change. Delete a semicolon, too, and read the error. Everyone sees that one a lot, and now it won't scare you.

New to Docker or the terminal? Tools has the setup.

In PHP

The functions you'll meet most before PHP Basics:

  • header() sets a response header, like the content type.
  • htmlspecialchars() makes text safe to put in a page. The XSS lesson is why.
  • print_r() prints an array so you can see what's in it.
  • isset() asks whether something exists and isn't null, PHP's word for no value.
  • trim() cuts the spaces off both ends of a string.
  • in_array() asks whether a value is in a list.

Gotchas

  • One = sets, three compare.$a = 1 puts 1 in $a. $a === 1 asks whether it's 1.
  • A missing semicolon is reported on the next line. PHP only notices when the next statement starts, so look one line up from the error.
  • Single quotes don't fill anything in.'Hello, $name\n' prints those exact characters, the backslash and n included.
  • Adding strings is an error.'Hello' + 'there' stops the script with a TypeError. Use the dot.

Try one line at a time

docker run --rm -it php:8.4-cli php -a opens PHP's interactive shell. Type echo strtoupper('hi');, press Enter, and see the answer. When a line in an example puzzles you, paste it in and poke at it. Type exit to leave.

That's enough PHP to read every example until PHP Basics. The semicolons will still get you. They get everyone. The course itself starts with the two programs at either end of every web page. Next: Client/Server.