In this chapter
We'll learn the rules for the names you give things in PHP, the words PHP keeps for itself, how professional PHP developers name variables, functions, classes and constants, and the shared code style that makes any PHP file easy to read — so your code looks like it was written by a professional from day one.
The Problem in Real Life
Marcus opens an old file that came with booking.php, written by a freelancer before Mia joined. $t = 25; $x2 = 2; $data_final_NEW = $t * $x2; — all squeezed onto one line. "What is $t?" he asks. Mia guesses: time? total? Tom? Nobody knows.
Determined to do better, Mia starts her own list of barbers and types $2ndBarber = "Ben";. PHP stops at once: Parse error: syntax error, unexpected integer "2". Then she tries $first-name = "Ben"; and gets another error.
"Names have rules," says Marcus. "Some are PHP's, and you can't break them. The rest are ours — and you shouldn't."
$2ndBarber looks like a perfectly normal name to me. What's wrong with it?
Mia
Names PHP Accepts vs. Names People Understand
Some names are illegal
PHP refuses names that start with a digit or contain spaces or dashes.
Some names are useless
$t and $x2 are legal, but nobody can tell what they hold.
Everyone writes differently
Without shared rules, every file looks different and every review becomes an argument about spaces.
Identifiers, Keywords and Code Style
Identifiers: an identifier is a name you choose for something in your code — a variable, a function, a constant, a class. $customerName, calculateTotal, MAX_BOOKINGS_PER_DAY are all identifiers. PHP has strict rules for what a valid name looks like:
- What it can contain: letters, digits and the underscore
_. - It can't start with a digit:
$slot2is fine,$2ndSlotis not. - It can't contain spaces or dashes:
$first nameand$first-nameare both errors — PHP reads the dash as a minus sign. - Variables start with a dollar sign: the
$isn't part of the name itself; it tells PHP "this is a variable". Functions, constants and classes have no$.
Valid and invalid names: the first code box below shows names PHP accepts. The next two show Mia's two mistakes and the errors they cause. Read the errors carefully: "unexpected integer 2" means PHP found a digit where a name had to start; "unexpected token =" means PHP read $first, then a minus sign, then a word — and an assignment made no sense after that. Letters from other languages (like $café) are technically allowed too, but always use plain English letters: every developer, keyboard and tool handles them.
<?php$customerName = "Zoë"; // letters$slot2 = "9:30"; // a digit, but not at the start$_internalCount = 0; // may start with _ (used rarely)echo "$customerName, $slot2";
<?php$2ndBarber = "Ben";// Parse error: syntax error, unexpected integer "2", expecting variable or "{" or "$"
Fix: $secondBarber or $barber2.
<?php$first-name = "Ben";// Parse error: syntax error, unexpected token "="
PHP reads $first - name. Fix: $firstName.
Reserved keywords: some words belong to PHP itself — if, else, echo, function, return, class, list, array, match, new and others. These are called reserved keywords, and you can't use them as the name of a function, class or constant: PHP would confuse your name with its own word. Variables are the exception — $class and $function work, because the $ makes it clear they're variables. Still, a name like $class is confusing to read, so choose something clearer, like $customerType.
<?phpfunction list() { // "list" is a reserved keywordreturn [];}// Parse error: syntax error, unexpected token "list", expecting "("$class = "VIP"; // works, because of the $ — but a clearer name is better
Naming conventions: PHP's rules only say what's allowed. On top of them, PHP developers everywhere follow shared naming conventions — habits for how to write names, so that one look at a name tells you what kind of thing it is. The table below shows them. Words are joined without spaces in one of three styles: camelCase (first word lower-case, each next word starts with a capital: totalPrice), PascalCase (every word starts with a capital: BookingService) and UPPER_SNAKE_CASE (all capitals, words joined with _: MAX_BOOKINGS_PER_DAY).
| What | Style | Example |
|---|---|---|
| Variables | camelCase | $customerName, $totalPrice, $isOpen |
| Functions | camelCase, start with a verb | calculateTotal(), findFreeSlots() |
| Constants | UPPER_SNAKE_CASE | MAX_BOOKINGS_PER_DAY, OPENING_HOUR |
| Classes (Act 11) | PascalCase, a noun | Booking, BookingService |
| Files with a class | Same as the class name | BookingService.php |
| Other PHP files | lower-case, words joined with - | booking.php, free-slots.php |
Meaningful names: a good name says what the value is, so nobody has to guess. Compare the freelancer's line with the same code written with real names, below — it does exactly the same thing, but now it explains itself. Some habits that help: say the whole word ($quantity, not $qty or $q); start true/false values with is, has or can ($isOpen, $hasPaid); and put the unit in the name when there is one ($durationMinutes, $priceInPence). A long, clear name is always better than a short, mysterious one.
<?php$t = 25; $x2 = 2; $data_final_NEW = $t * $x2; echo $data_final_NEW;
<?php$haircutPrice = 25;$numberOfPeople = 2;$totalPrice = $haircutPrice * $numberOfPeople;echo $totalPrice; // 50
Same result, 50 — but now anyone can read what it means.
Code style: code style is how code is laid out — spaces, line breaks, where braces go. As you saw in chapter 3, PHP doesn't care. People do. The PHP community shares one style guide, called the PER Coding Style (it replaced the older "PSR-12"; you'll see both names). Its main rules: indent with 4 spaces; one statement per line; spaces around operators like =, + and >; the opening brace of a function goes on its own line, but the opening brace of if, loops and other blocks stays on the same line; and keep lines shorter than about 120 characters. The two code boxes below do exactly the same thing — one in no style at all, one in PER style.
<?phpfunction CALC($p,$q){if($p>0){return $p*$q;}else{return 0;}}echo CALC(25,2);
<?phpfunction calculateTotal(int $price, int $quantity): int{if ($price > 0) {return $price * $quantity;}return 0;}echo calculateTotal(25, 2); // 50
Function brace on its own line, if brace on the same line, 4-space indents, spaces around operators — and a name that says what it does.
Let a tool do it: nobody formats code by hand at work. A tool called PHP-CS-Fixer rewrites your files into the agreed style automatically, in a second — you'll set it up in Act 21. Until then, copy the shape of the examples in this course, and your code will already look like a professional's.
Key Takeaway
Identifiers are the names you choose: letters, digits and underscores, never starting with a digit, and never a reserved keyword for functions or classes. Beyond PHP's rules, follow the conventions — camelCase for variables and functions, PascalCase for classes, UPPER_SNAKE_CASE for constants — choose names that say what a value is, and lay code out in the shared PER style.
Why This Matters
Code is read far more often than it's written. Every name Mia chooses will be read by Marcus in reviews, by the next developer BookBay hires, and by herself in six months. Clear names and one shared style make the code explain itself — and make bugs like $Slots vs $slots (chapter 3) almost impossible.
It also matters for jobs: in an interview or a code review, clean names and consistent formatting are the first thing people notice — long before they read what the code does.
Mia renames everything in her barber list. Now the names are clear — but she still can't see the list on the page properly. The text runs together on one line, and one value doesn't show at all. "How do I actually print things the right way?" That's the next chapter: echo and print.
