Operators
Arithmetic operators
$sum = 10 + 5; // 15
$diff = 10 - 5; // 5
$product = 10 * 5; // 50
$quotient = 10 / 5; // 2
$remainder = 10 % 3; // 1
$power = 2 ** 3; // 8
The + operator always performs numeric addition, even on strings. "5" + 10 evaluates to 15, not "510". Use the . operator for string concatenation.
String concatenation
Use the . operator to join strings:
$first = "Hello";
$second = "World";
$combined = $first . " " . $second; // "Hello World"
The .= compound assignment appends to a string:
$message = "Hello";
$message .= " World"; // "Hello World"
For embedding variables, string interpolation in double-quoted or backtick strings is often cleaner:
$name = "Alice";
$greeting = "Hello, $name!"; // "Hello, Alice!"
String interpolation
Embed variables directly in double-quoted or backtick strings:
$name = "World";
$message = "Hello, $name!"; // "Hello, World!"
$count = 42;
$status = "You have $count items";
Object properties are also interpolated:
$person = [name: "John", age: 30];
$info = "$person.name is $person.age years old";
// "John is 30 years old"
Single-quoted strings ('...') do not support interpolation. Use double quotes or backticks when you need variable expansion.
String subscript access
Access individual characters or substrings using bracket notation:
$str = "Hello";
echo $str[0]; // "H" (first character)
echo $str[0, 3]; // "Hel" (substring from position 0, length 3)
Assignment operators
$x = 10; // Simple assignment
$x += 5; // $x = $x + 5 (numeric addition)
$x -= 3; // $x = $x - 3
$x *= 2; // $x = $x * 2
$x /= 4; // $x = $x / 4
$x %= 3; // $x = $x % 3
$x .= "!"; // $x = $x . "!" (string concatenation)
Comparison operators
$equal = (10 == 10); // true
$notEqual = (10 != 5); // true
$greater = (10 > 5); // true
$less = (10 < 20); // true
$greaterEqual = (10 >= 10); // true
$lessEqual = (10 <= 10); // true
// Strict comparison (checks type as well as value)
$strictEqual = (10 === 10); // true
$strictNotEqual = (10 !== "10"); // true (int vs string)
// Spaceship operator (returns -1, 0, or 1)
$cmp = (5 <=> 10); // -1
Regex match operator
Use ~= to test a string against a regular expression:
$email = "[email protected]";
if ($email ~= "/^[^@]+@[^@]+$/") {
echo "Valid email format";
}
Logical operators
$and = (true && false); // false
$or = (true || false); // true
$not = !true; // false
Null coalescing operator (??)
Provide default values for null or undefined variables:
$username = $inputName ?? "Guest";
$count = $maybeCount ?? 0;
Null coalescing assignment (?=)
Set a variable only if it is currently null or undefined:
$optional = null;
$optional ?= "default value";
// $optional = "default value"
$alreadySet = "existing";
$alreadySet ?= "new value";
// $alreadySet = "existing" (unchanged)
Increment/decrement operators
$x = 5;
$x++; // $x becomes 6 (post-increment)
++$x; // $x becomes 7 (pre-increment)
$x--; // $x becomes 6 (post-decrement)
--$x; // $x becomes 5 (pre-decrement)
The clone operator
Creates a shallow copy of an array or object:
$original = [1, 2, 3];
$copy = clone $original;
$copy[] = 4;
// $original still has 3 elements; $copy has 4
The delete operator
Removes a property from an object or element from an array:
$data = [name: "John", age: 30, temp: true];
delete $data.temp;
// $data now only has name and age
The in operator
Checks whether a key exists in an array or object:
$config = [debug: true, level: 3];
if ("debug" in $config) {
echo "Debug key exists";
}
Bitwise operators
echo 5 & 3; // 1 (AND)
echo 5 | 3; // 7 (OR)
echo 5 ^ 3; // 6 (XOR)
echo ~5; // -6 (NOT)
echo 1 << 3; // 8 (left shift)
echo 16 >> 2; // 4 (right shift)
Comments
Single-line comments
// This is a comment
$name = "John"; // Comment after code
Multi-line comments
/*
* This is a multi-line comment
* that spans multiple lines
*/
$active = true;
Operator precedence
Operators are evaluated in this order (highest to lowest):
()— Parentheses++--— Increment/decrement!~-(unary)+(unary) — NOT, bitwise NOT, negate, numeric coercion**— Exponentiation*/%— Multiplication, division, modulus+-— Addition, subtraction.— String concatenation<<>>— Bitwise shift<<=>>=— Comparison==!====!==~=— Equality, regex match&— Bitwise AND^— Bitwise XOR|— Bitwise OR&&— Logical AND||— Logical OR??— Null coalescing=+=-=.=?=etc. — Assignment
Use parentheses to control evaluation order:
$result = (10 + 5) * 2; // 30, not 20