Variables, Types, and String Interpolation
PHP's `$variable` syntax, loose typing, and interpolating values into strings.
What you'll learn
- Declare a variable using PHP's `$` sigil
- Distinguish single-quoted (literal) strings from double-quoted (interpolating) strings
- Predict how PHP's loose typing handles arithmetic and concatenation involving numeric strings
Explanation
Every PHP variable name starts with a $ sigil: $age = 30;. Unlike some languages, you never declare a variable's type up front -- PHP figures it out from the assigned value, and a variable can hold a different type later if you reassign it (this is PHP's loose typing; you can opt into stricter behavior per-file with declare(strict_types=1);, which mainly affects function argument/return type coercion rather than variable assignment itself).
PHP has two everyday string quoting styles that behave differently: a single-quoted string ('Hello, $name') is almost entirely literal -- $name stays as the literal text $name, not the variable's value. A double-quoted string ("Hello, $name") interpolates -- it substitutes the variable's actual value directly into the string, and also recognizes escape sequences like \n.
PHP's loose typing extends to numeric strings: a string that looks like a number, such as "85", can be used directly in arithmetic -- "85" + 10 evaluates to the integer 95, with PHP converting the string to a number first. This is different from the . (dot) concatenation operator, which always converts its operands to strings and joins them -- "85" . 10 produces the string "8510", not a sum.
Getting + (arithmetic, numeric coercion) and . (concatenation, string coercion) confused is one of the most common early PHP mistakes, especially coming from a language like JavaScript where + does both jobs depending on the operand types.
Guided lab
Predict: Loose typing with numeric strings
Read this script and predict exactly what it sends to the browser.
<?php
$name = "Ada";
$age = 30;
$score = "85";
$bonus = 10;
echo "$name is $age years old.\n";
echo "Total score: " . ($score + $bonus) . "\n";
echo "Score as string: " . $score . $bonus . "\n";Stuck? Get a hint.
Common mistakes
- Forgetting the `$` sigil and writing a bare identifier where PHP expects a variable name.
- Using a single-quoted string when interpolation was intended, then being confused why `'Hello, $name'` prints the literal text `$name` instead of its value.
- Confusing `+` (numeric addition, converting operands to numbers) with `.` (string concatenation, converting operands to strings) -- they are not interchangeable the way JavaScript's `+` can be.
Knowledge check
Takeaway
Use double-quoted strings when you want variable interpolation, and remember `+` coerces to numbers while `.` coerces to strings for concatenation.
Summary
PHP variables use a `$` sigil and are loosely typed; double-quoted strings interpolate values and escape sequences, single-quoted strings don't; `+` and `.` coerce operands differently.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.