What you'll learn
Operators turn values into expressions; strings turn results into useful messages. This lesson focuses on predictable arithmetic, strict comparison, short-circuit logic, and readable text.
- Build numeric expressions and update values
- Compare values without accidental coercion
- Combine conditions and provide safe defaults
- Choose the right quoting and interpolation style
- Use common Unicode-aware string workflows
Arithmetic and assignment
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 2 + 3 |
- | Subtraction | 10 - 4 |
* | Multiplication | 6 * 7 |
/ | Division | 7 / 2 |
% | Remainder | 7 % 2 |
** | Exponent | 2 ** 8 |
<?php
$unitPrice = 12.50;
$quantity = 3;
$discount = 5;
$subtotal = $unitPrice * $quantity;
$total = $subtotal - $discount;
echo "Subtotal: $" . number_format($subtotal, 2) . PHP_EOL;
echo "Total: $" . number_format($total, 2);Compound assignment—such as $count += 1 or $name .= "!"—updates a variable using its current value. Parentheses make precedence and business intent obvious.
Prefer strict comparison
== compares after PHP type coercion; === requires both value and type to match. Normalize boundary data, then use strict comparison for application logic.
<?php
$submittedId = "42";
$recordId = 42;
var_dump($submittedId == $recordId); // true: values coerce
var_dump($submittedId === $recordId); // false: types differ
$normalizedId = (int) $submittedId;
var_dump($normalizedId === $recordId); // trueLoose comparison surprises
PHP also provides !==, <, <=, >, >=, and the spaceship operator <=>, which returns -1, 0, or 1 for sorting.
Logic, short-circuiting, and defaults
<?php
$isSignedIn = true;
$isActive = true;
$role = null;
$canEnter = $isSignedIn && $isActive;
$displayRole = $role ?? 'member';
echo $canEnter ? "Access granted to {$displayRole}" : "Access denied";&& stops when the left side is false; || stops when the left side is true. This short-circuit behavior is useful for guards. The null-coalescing operator ?? provides a default only when a value is missing or null.
Tip
&& and || over the lower-precedence words and and or in ordinary expressions; assignment combined with the word forms can be surprising.Build readable strings
Single-quoted strings are mostly literal. Double-quoted strings interpret escapes and interpolate variables. Use . for explicit concatenation and braces when interpolation would be ambiguous.
<?php
$course = 'PHP';
$module = 3;
echo 'Learning $course' . PHP_EOL;
echo "Learning {$course}, module {$module}" . PHP_EOL;
echo "Path: C:\courses\php";Essential string functions
<?php
$rawTitle = " Learn Modern PHP ";
$title = trim($rawTitle);
$slug = strtolower(str_replace(' ', '-', $title));
echo $title . PHP_EOL;
echo $slug . PHP_EOL;
echo strlen($slug);Unicode matters
strlen() count bytes. For human text containing non-ASCII characters, enable the mbstring extension and use functions such as mb_strlen() and mb_strtolower().Recap & quick check
Key takeaways
- Use parentheses to make multi-step numeric intent obvious.
- Normalize boundary data, then compare with === and !==.
- && and || short-circuit; ?? supplies a default for missing or null values.
- Double quotes interpolate, single quotes are mostly literal, and . concatenates.
- Choose multibyte string functions when processing human Unicode text.
Quick check
1. Which operator compares both value and type?
2. Which operator concatenates PHP strings?
3. What does ?? provide?