PHP Syntax & Variables
Variables, types, string interpolation, indexed and associative arrays, control flow, and the match expression.
Variables
Every PHP variable name starts with $. PHP is dynamically typed by default — a variable's type is determined by the value currently assigned to it, and can change:
<?php
$name = "Ada"; // string
$age = 30; // int
$price = 19.99; // float
$isActive = true; // bool
$age = "thirty"; // legal — $age now holds a string instead of an int
Even though PHP is dynamically typed, you should still add explicit type declarations wherever possible (covered on the next page) — relying on implicit, ever-changing types is a common source of bugs in larger codebases.
Types
<?php
var_dump(42); // int(42)
var_dump(3.14); // float(3.14)
var_dump("hello"); // string(5) "hello"
var_dump(true); // bool(true)
var_dump(null); // NULL
var_dump([1, 2, 3]); // array(3) { ... }
var_dump is invaluable while learning and debugging — it prints a value's actual type alongside its content, which echo alone never shows you.
String interpolation
Double-quoted strings interpolate variables directly; single-quoted strings never do (and are marginally faster, since PHP doesn't have to scan them for variables):
<?php
$name = "Ada";
$age = 30;
echo "Hello, $name! You are $age years old.\n"; // Hello, Ada! You are 30 years old.
echo 'Hello, $name!' . "\n"; // Hello, $name! — literal, no interpolation
// Complex expressions need curly braces
$user = ["name" => "Ada"];
echo "Welcome, {$user['name']}!\n"; // Welcome, Ada!
For multi-line strings with interpolation, a heredoc is often more readable than concatenating quoted strings:
<?php
$name = "Ada";
$message = <<<EOT
Hello, {$name}!
This is a multi-line heredoc string.
EOT;
echo $message;
Arrays
PHP has one array type that does the job of both a list and a hash map, depending on how you use it.
Indexed arrays (sequential, zero-based keys):
<?php
$fruits = ["apple", "banana", "cherry"];
echo $fruits[0]; // apple
$fruits[] = "date"; // append — $fruits now has 4 elements
echo count($fruits); // 4
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
Associative arrays (explicit string/int keys):
<?php
$user = [
"name" => "Ada",
"age" => 30,
"role" => "engineer",
];
echo $user["name"]; // Ada
$user["role"] = "lead engineer"; // update an existing key
foreach ($user as $key => $value) {
echo "$key: $value\n";
}
// name: Ada
// age: 30
// role: lead engineer
Common array functions you'll reach for constantly: array_map, array_filter, array_reduce, in_array, array_key_exists, sort/usort.
<?php
$numbers = [1, 2, 3, 4, 5];
$doubled = array_map(fn($n) => $n * 2, $numbers); // [2, 4, 6, 8, 10]
$evens = array_filter($numbers, fn($n) => $n % 2 === 0); // [1 => 2, 3 => 4] — keys are preserved!
$total = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0); // 15
Note that array_filter preserves the original keys of the elements that pass the filter — use array_values() on the result if you need a clean, re-indexed array afterwards.
Control flow
<?php
$n = 7;
if ($n % 2 === 0) {
echo "even";
} elseif ($n < 0) {
echo "negative";
} else {
echo "odd";
}
for ($i = 0; $i < 3; $i++) {
echo $i; // 012
}
$i = 0;
while ($i < 3) {
echo $i;
$i++;
}
The match expression (PHP 8+)
match is a more modern, more predictable alternative to switch: it uses strict (===) comparison, requires no break statements, and is itself an expression — it evaluates to a value you can assign or return directly.
<?php
$statusCode = 404;
$message = match ($statusCode) {
200, 201 => "Success", // multiple values can share one arm
404 => "Not Found",
500 => "Server Error",
default => "Unknown Status",
};
echo $message; // Not Found
Compare that to the older switch statement, which uses loose (==) comparison by default and requires explicit breaks to avoid falling through into the next case:
<?php
switch ($statusCode) {
case 200:
case 201:
$message = "Success";
break;
case 404:
$message = "Not Found";
break;
default:
$message = "Unknown Status";
break; // easy to forget — a missing break falls through to the next case
}
Common mistakes
- Forgetting
switchuses loose (==) comparison —case "0":can match0,false, or an empty string in surprising ways.matchavoids this entirely by using strict comparison. - Forgetting a
breakin aswitchstatement, causing execution to "fall through" into the next case unintentionally. - Using single quotes when you actually need variable interpolation (or vice versa — using double quotes needlessly, which is a tiny performance cost at scale).
- Assuming
array_filterre-indexes the array afterward — it preserves original keys, which can produce a "gappy" array.
Interview questions
Q: What's the difference between match and switch?
match uses strict (===) comparison, requires no break (there's no fallthrough), and is an expression that returns a value directly. switch uses loose (==) comparison by default, requires explicit break statements to prevent fallthrough between cases, and is a statement, not an expression — you can't assign its result directly to a variable.
Q: What's the difference between single-quoted and double-quoted strings in PHP?
Double-quoted strings interpolate variables and process escape sequences like \n; single-quoted strings treat their content almost entirely literally (only \\ and \' are special), so 'Hello, $name' prints the literal text $name instead of substituting a variable's value.