In this chapter
We'll learn how to leave notes in code with comments (and what makes a comment useful), why spaces and line breaks mostly don't matter to PHP, and the one rule that catches every beginner: $price and $Price are two different variables — but echo and ECHO are the same.
The Problem in Real Life
Reading booking.php from the top, Mia keeps meeting lines that aren't code at all. // Tom asked for 30-minute slots. # old version below. A whole paragraph wrapped in /* … */. And one that says // TODO: fix before Christmas — but nobody remembers which Christmas.
Then a real bug. Near the bottom, the page should print how many slots are free, but it prints nothing. PHP shows a warning: Undefined variable $Slots. Mia scrolls up — the variable is right there, $slots = 6;, a few lines above.
"It's defined! It's the same word!" Marcus points at one letter. "Almost the same word. Capital S."
$slots and $Slots — PHP really thinks those are two different things?
Mia
Notes for People vs. Instructions for PHP
Notes inside the code
Code needs explanations for the next developer — without PHP trying to run them.
Notes can lie
A comment that was true years ago and never updated is worse than no comment.
One capital letter
Some names in PHP care about upper and lower case, and some don't.
Comments, Whitespace and Case Sensitivity
Comments: a comment is text in your code that PHP completely ignores. It's written for people — the next developer, or you in three months — to explain what the code is for, or why it's written a certain way. PHP skips it as if it weren't there.
PHP has three kinds of comments:
- Single-line comments with //: everything from
//to the end of that line is a comment. It can sit on its own line or at the end of a line of code. This is the one you'll use most. - Single-line comments with #: works the same as
//. It's older and less common today — and in PHP 8 it has one trap, shown below. - Multi-line comments: everything between
/*and*/is a comment, even across many lines. Useful for a longer explanation.
Single-line comments: the code below uses all three kinds. Notice the comment at the end of the $opensAt line: the code before // still runs, only the rest of the line is ignored. The output is just Open at 9:00 — none of the comments appear anywhere.
<?php// Opening hours for Tom's Barbershop$opensAt = "9:00"; // the first slot of the day# This is a comment too, but // is more common/*Closed on Sundays.Bank holidays: ask Tom first.*/echo "Open at $opensAt";
Output: Open at 9:00
Multi-line comments can't be nested: a /* … */ comment ends at the first */ PHP finds. So you can't put one multi-line comment inside another: in the code below, the comment ends after inner */, and PHP tries to run still a comment? */ as code — a parse error. If you need to switch off a block of code that already contains a /* */ comment, use // on each line instead.
<?php/* outer /* inner */ still a comment? */echo "Hi";// Parse error: syntax error, unexpected identifier "a" in main.php on line 2
Doc comments: a comment that starts with /** (two stars) is called a doc comment or docblock. It's still a comment to PHP, but editors and tools read it to show help about the code below it — usually a function or class. You'll write them from Act 09 on; for now, just recognise the shape.
<?php/*** Works out the price of a booking, with the booking fee added.*/function bookingPrice(int $base): int{return $base + 2;}echo bookingPrice(25); // 27
What makes a comment useful: good comments explain why, not what. The code already says what it does — $price = $price * 0.9; clearly multiplies by 0.9. A useful comment says why: // 10% loyalty discount, agreed with Tom. And keep comments true: when you change code, change its comment. A comment like // TODO: fix before Christmas that nobody updated just confuses the next reader. Comments are also handy while testing: putting // in front of a line "switches it off" without deleting it, as below. Just don't leave switched-off code lying around once you're done — Git (Act 01's workflow) remembers old versions for you.
<?php$price = 25;// $price = $price * 0.9; // 10% loyalty discount, switched off for nowecho $price; // 25
Two traps with single-line comments: first, a // or # comment ends at the end of the line — or at a closing tag ?>, whichever comes first. In the first code box below, the ?> inside the comment switches PHP back to text mode, and the rest of the file is printed as plain text. Second, in PHP 8 the characters #[ start an attribute (a special label for code, which you'll meet in Act 12) — so a comment written as #[like this] is a parse error. Use // for comments and you'll never hit either problem.
<?phpecho "Hello"; // a closing tag ?> ends PHP even hereecho "World";
Output: Hello ends PHP even here, then the text echo "World"; printed as plain text — PHP stopped running code at the ?>.
<?php#[this is not a comment]echo "Hi";// Parse error: syntax error, unexpected identifier "is", expecting "]"
Whitespace: whitespace means spaces, tabs and line breaks. Between the parts of your code, PHP ignores it: $total=25+10;echo $total; on one line works exactly like a neatly spaced version. But whitespace inside quotes is part of the text and is kept exactly — four spaces between two words print as four spaces. And PHP still needs at least one space between two words that would otherwise run together, like <?php and echo. Since PHP doesn't care, the spacing is entirely for humans — which is why teams agree on one style (chapter 4).
<?php$total=25+10;echo $total; // 35 — works, but hard to readecho "\n";$total = 25 + 10; // the same, readableecho $total;echo "\n";echo "Tom's Barbershop"; // the 4 spaces inside quotes are printed
Output: 35, then 35, then Tom's Barbershop — with all four spaces.
Case sensitivity — variables: case means upper-case (capital) or lower-case letters. Variable names are case-sensitive: $slots, $Slots and $SLOTS are three completely different variables. That was Mia's bug: $slots was set, $Slots was never set, so PHP warned "Undefined variable $Slots" and printed nothing. The fix isn't to remember capitals — it's to follow one naming style everywhere, so there's only ever one way a name can be written.
<?php$slots = 6;echo $Slots;// Warning: Undefined variable $Slots in main.php on line 4
PHP prints a warning and an empty value. Change $Slots to $slots and it prints 6.
Case sensitivity — everything else: PHP's own keywords (like echo, if, true) and the names of functions are not case-sensitive: ECHO, Echo and echo all work, and so do strlen() and STRLEN(). But constants (fixed values, which you'll meet in Act 03) are case-sensitive. The table below sums it up. Even where PHP doesn't care, always write keywords and function names in lower case — that's what every PHP developer expects to read.
| Kind of name | Case-sensitive? | Example |
|---|---|---|
| Variables | Yes | $slots and $Slots are different |
| Constants | Yes | SHOP_NAME and shop_name are different |
| Keywords | No | echo, ECHO and Echo all work |
| Function names | No | strlen() and STRLEN() both work |
| true, false, null | No | true and TRUE both work |
Even where it's "No", write it in lower case — it's what everyone expects.
<?phpECHO "Keywords don't care about case\n";echo STRLEN("Tom"), "\n"; // 3const SHOP_NAME = "Tom's Barbershop";echo SHOP_NAME, "\n";echo shop_name; // Error: Undefined constant "shop_name"
Key Takeaway
Comments are notes PHP ignores: // and # for one line, /* */ for several, /** */ for docs. Spaces and line breaks don't change what code does, except inside quotes. Variable names and constants are case-sensitive; keywords and function names aren't — but write them in lower case anyway.
Why This Matters
BookBay is a small team now, but code lives for years. Comments that explain why are how Mia — and whoever joins after her — will understand decisions nobody remembers making. Outdated comments and switched-off code, on the other hand, are how a codebase slowly becomes confusing.
The case rule matters every day: "Undefined variable" from one wrong capital letter is one of the most common beginner bugs. A consistent naming style (next chapter) makes it almost impossible.
Mia fixes $Slots, deletes the Christmas comment and asks Marcus what the team's rule is for naming things. He opens a file with names like $t, $x2 and $data_final_NEW. "This is what happens without rules. Let's set some." That's the next chapter.
