---
title: "Operators"
description: "Arithmetic, string, comparison, logical, and special operators, plus string interpolation, comments, and operator precedence."
canonical_url: "https://www.zeyos.com/zymba/zymba-guide/language-basics/operators"
markdown_url: "https://www.zeyos.com/ai/guide/zymba/zymba-guide/language-basics/operators.md"
section: "zymba-guide"
authority: "draft-guide"
language: "en"
source_repository: "zeyos/guide"
source_path: "docs/zymba/02-language-basics/operators.md"
source_revision: "cce886a8da98cd124ee3644711085bbdcf1a87ce"
---

# 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 = "user@example.com";
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
```
