Phase 1 · PHP FoundationsModule 3~45 min read

Operators, Expressions & Strings

Calculate, compare, combine, and format values while avoiding PHP's most common comparison and string pitfalls.

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

OperatorMeaningExample
+Addition2 + 3
-Subtraction10 - 4
*Multiplication6 * 7
/Division7 / 2
%Remainder7 % 2
**Exponent2 ** 8
invoice.php
<?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.

compare.php
<?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); // true

Loose comparison surprises

Avoid relying on PHP to decide whether text, numbers, booleans, and null are equivalent. Strict comparison makes the rule visible and protects refactoring.

PHP also provides !==, <, <=, >, >=, and the spaceship operator <=>, which returns -1, 0, or 1 for sorting.

Logic, short-circuiting, and defaults

access.php
<?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

Prefer && 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.

strings.php
<?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

normalize.php
<?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

Functions such as 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?