Skip to main content

Operators

Arithmetic operators

ZYMBA
$sum = 10 + 5; // 15
$diff = 10 - 5; // 5
$product = 10 * 5; // 50
$quotient = 10 / 5; // 2
$remainder = 10 % 3; // 1
$power = 2 ** 3; // 8
warning

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:

ZYMBA
$first = "Hello";
$second = "World";
$combined = $first . " " . $second; // "Hello World"

The .= compound assignment appends to a string:

ZYMBA
$message = "Hello";
$message .= " World"; // "Hello World"

For embedding variables, string interpolation in double-quoted or backtick strings is often cleaner:

ZYMBA
$name = "Alice";
$greeting = "Hello, $name!"; // "Hello, Alice!"

String interpolation

Embed variables directly in double-quoted or backtick strings:

ZYMBA
$name = "World";
$message = "Hello, $name!"; // "Hello, World!"

$count = 42;
$status = "You have $count items";

Object properties are also interpolated:

ZYMBA
$person = [name: "John", age: 30];
$info = "$person.name is $person.age years old";
// "John is 30 years old"
note

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:

ZYMBA
$str = "Hello";
echo $str[0]; // "H" (first character)
echo $str[0, 3]; // "Hel" (substring from position 0, length 3)

Assignment operators

ZYMBA
$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

ZYMBA
$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:

ZYMBA
$email = "[email protected]";
if ($email ~= "/^[^@]+@[^@]+$/") {
echo "Valid email format";
}

Logical operators

ZYMBA
$and = (true && false); // false
$or = (true || false); // true
$not = !true; // false

Null coalescing operator (??)

Provide default values for null or undefined variables:

ZYMBA
$username = $inputName ?? "Guest";
$count = $maybeCount ?? 0;

Null coalescing assignment (?=)

Set a variable only if it is currently null or undefined:

ZYMBA
$optional = null;
$optional ?= "default value";
// $optional = "default value"

$alreadySet = "existing";
$alreadySet ?= "new value";
// $alreadySet = "existing" (unchanged)

Increment/decrement operators

ZYMBA
$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:

ZYMBA
$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:

ZYMBA
$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:

ZYMBA
$config = [debug: true, level: 3];
if ("debug" in $config) {
echo "Debug key exists";
}

Bitwise operators

ZYMBA
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

ZYMBA
// This is a comment
$name = "John"; // Comment after code

Multi-line comments

ZYMBA
/*
* This is a multi-line comment
* that spans multiple lines
*/
$active = true;

Operator precedence

Operators are evaluated in this order (highest to lowest):

  1. () — Parentheses
  2. ++ -- — Increment/decrement
  3. ! ~ - (unary) + (unary) — NOT, bitwise NOT, negate, numeric coercion
  4. ** — Exponentiation
  5. * / % — Multiplication, division, modulus
  6. + - — Addition, subtraction
  7. . — String concatenation
  8. << >> — Bitwise shift
  9. < <= > >= — Comparison
  10. == != === !== ~= — Equality, regex match
  11. & — Bitwise AND
  12. ^ — Bitwise XOR
  13. | — Bitwise OR
  14. && — Logical AND
  15. || — Logical OR
  16. ?? — Null coalescing
  17. = += -= .= ?= etc. — Assignment

Use parentheses to control evaluation order:

ZYMBA
$result = (10 + 5) * 2; // 30, not 20