Skip to content

Precedence

Precedence is the order in which operators run. * runs before +, so 1 + 2 * 3 is 7.

Which operator runs first

Precedence decides which operator runs first. Associativity decides the order when two operators of the same strength meet.

Novis uses the order most languages use: * and / before + and -, then the comparisons, then &&, then ||, and the assignment last. ** runs before a minus sign in front of it, and a chain of them is read from the right. So -2 ** 2 is -4 and 2 ** 3 ** 2 is 512. A conversion with as binds tighter than every operator around it.

Good to know: the compiler gives warning W1022 for two mixes that are easy to misread. In $n ?? 0 + 10, the + runs first, so the default is 10.

The examples below show the arithmetic order, ** with a minus sign, and as and . in a line of text. The last one shows ! with is, the pipeline |> and a method call on new.

Multiplication before addition

<?nvs
// Shows the order Novis uses when you write several operators in one
// expression: multiplication before addition, and the comparison after both.
int $price = 20;
int $quantity = 3;
int $shipping = 5;
// `*` runs before `+`, so this is `20 * 3 + 5`. It prints 65.
echo $price * $quantity + $shipping, "\n";
// Parentheses change the order, and cost nothing when the program runs.
// This prints 160.
echo $price * ($quantity + $shipping), "\n";
// The arithmetic and the comparison run before `&&`, and `&&` runs before
// `||`. This prints `free shipping`.
bool $isMember = false;
bool $hasCoupon = true;
echo $isMember || $hasCoupon && $price * $quantity > 50 ? 'free shipping' : 'pay shipping', "\n";
Output
65
160
free shipping

The power operator and a minus sign

<?nvs
// Shows the two rules for `**`: it runs before a minus sign written in front
// of it, and a chain of them is read from the right.
// `2 ** 2` runs first, and the minus sign is applied to the result.
// This prints -4.
echo -2 ** 2, "\n";
// Parentheses make the minus sign part of the number. This prints 4.
echo (-2) ** 2, "\n";
// A chain is read from the right: `3 ** 2` is 9, and `2 ** 9` is 512.
echo 2 ** 3 ** 2, "\n";
// The same chain read from the left would be 64. Write the parentheses when
// you mean that. This prints 64.
echo (2 ** 3) ** 2, "\n";
Output
-4
4
512
64

Building a total line from a form field

<?nvs
// Shows the two rules you meet when you build a line of text out of numbers:
// `as` binds tighter than every operator around it, and the dot that joins
// two strings binds looser than the arithmetic.
// The quantity arrived from a form, so it is text.
string $typed = '3';
int $unitPrice = 20;
int $shipping = 5;
// `as int` converts before the multiplication, so no parentheses are needed
// here. This prints 60.
echo $typed as int * $unitPrice, "\n";
// The whole sum is worked out first and then joined to the label, because `.`
// binds looser than `*` and `+`. This prints `total: 65`.
echo 'total: ' . $typed as int * $unitPrice + $shipping, "\n";
// A division needs its own parentheses, because `as` would otherwise convert
// only the number on its right. This prints 21.
int $cents = 4200;
echo ($cents / 200) as int, "\n";
Output
60
total: 65
21

Is the pipeline and a method call on new

<?nvs
// Shows the order of `!` with `is`, of `|>` with other operators, and of a method call on `new`.
class Coupon
{
public function constructor(public int $percent)
{}
public function label(): string
{
return $this->percent . '% off';
}
}
// `is` runs before `!`, so this tests "not a `Coupon`". This prints `false`.
mixed $value = new Coupon(10);
echo !$value is Coupon ? 'true' : 'false', "\n";
// The minus sign runs before `|>`, so the pipeline gets -5. This prints 5.
int $change = 5;
echo -$change |> Core\Math::abs($_), "\n";
// `|>` runs before `.`, so only `$code` goes into the pipeline. This prints `code: SPRING`.
string $code = 'spring';
echo 'code: ' . $code |> Core\Str::upper($_), "\n";
// `<<` runs before `.`, so `1 << 2` is 4 and is then joined to "x". This prints `4x`.
echo 1 << 2 . 'x', "\n";
// A method call on `new` needs no parentheses. This prints `25% off`.
echo new Coupon(25)->label(), "\n";
// A ternary inside another one groups to the right. This prints `small`.
int $total = 40;
echo $total > 100 ? 'large' : $total > 20 ? 'small' : 'tiny', "\n";
Output
false
5
code: SPRING
4x
25% off
small

Details

The full order, from the operators that run first to the ones that run last:

  • ->, ?->, ::, […], a call (…), $x++, $x-- and as T, left to right
  • new and clone
  • **, right to left
  • a prefix -, +, ~, ++ and --
  • |>, left to right
  • is, left to right
  • !
  • *, / and %, left to right
  • + and -, left to right
  • << and >>, left to right
  • ., left to right
  • <, <=, > and >=, left to right
  • ==, != and <=>, left to right
  • &, then ^, then |, each left to right
  • &&, then ||, each left to right
  • ??, right to left
  • ? :, right to left
  • catch (T $e) => … in an expression
  • = and every other assignment, such as += and ??=, right to left
  • print, throw and yield, which take everything to their right

as runs before every prefix and binary operator. -$s as int is -($s as int), and $s as int ** 2 is ($s as int) ** 2. To convert a result, write ($a / $b) as int.

! runs after is. !$o is User is !($o is User).

|> runs before every binary operator and after a prefix operator. -$a |> Core\Math::abs($_) is Core\Math::abs(-$a). In "x=" . $a |> Core\Str::upper($_), only $a goes into the pipeline. In $x = $a |> Core\Str::trim($_), $x gets the result of the whole pipeline.

new User()->name() calls the method on the new object, with no parentheses around new. clone $a->b copies the object in the property $a->b.

The compiler gives warning W1022 in three cases:

  • a ?? whose default is an arithmetic or . expression without parentheses, such as $n ?? 0 + 10
  • a ? : whose condition is an arithmetic or . expression without parentheses, such as "n=" . $ok ? "x" : "y"
  • a ?: or ? : whose condition is a ?? without parentheses, such as $m ?? 1 ?: 2

The warning shows two ways to add the parentheses. The first keeps what the line does now. The second is the order the line most likely meant. A warning does not stop the program.

A catch in an expression runs after the ternary and before the assignment. So in $x = $a / $b catch (ArithmeticError) => 0, the catch covers the whole division, and $x gets the result. A second catch after it is another case of the same catch expression.

Operators that do not exist

Some operators do not exist in Novis. Writing one is a compile error, and most of these errors name what to write instead.

&& and || are the only two logical operators that join conditions. and and or do not exist, and xor is != between two bool values. == and != never convert either side, so ===, !== and <> do not compile. A cast such as (int)$x is written $x as int. @ does not exist, because a failure is thrown and you catch it. There are no references (&$x), no variable variables ($$name) and no backticks. To add two arrays, use Core\Arr::underlay. To read one character, use Core\Str::at. A string has no ++.

Good to know: every one of these errors appears when you compile, never while the program runs.

The examples below show the replacements you need most often: && and || in a condition, == for equality, and Core\Arr::underlay and Core\Str::at. The last one shows the error for ===.

And or and xor written with symbols

<?nvs
// Shows how to write "and", "or" and "xor" between two conditions. Use `&&` for
// "and" and `||` for "or". For "xor" on two `bool` values, use `!=`.
bool $paid = true;
bool $inStock = true;
bool $expressRequested = false;
bool $customerIsPremium = true;
// `&&` is true when both sides are true. This prints `ship now`.
echo $paid && $inStock ? 'ship now' : 'hold', "\n";
// `||` is true when at least one side is true. This prints `express`.
echo $expressRequested || $customerIsPremium ? 'express' : 'standard', "\n";
// `!=` between two `bool` values is true when exactly one of them is true.
// This prints `exactly one`.
echo $expressRequested != $customerIsPremium ? 'exactly one' : 'both or neither', "\n";
Output
ship now
express
exactly one

One equality operator

<?nvs
// Shows the one equality operator. `==` never converts its operands.
// `===`, `!==` and `<>` do not exist, and none of them compiles.
// A quantity that arrived as text, and the quantity already stored.
string $typed = '7';
int $stored = 7;
// Convert the text with `as int` first, then compare. This prints `same`.
echo ($typed as int) == $stored ? 'same' : 'different', "\n";
// Two strings compare character by character, so `"07"` and `"7"` are two
// different strings. This prints `no match`.
string $fromUrl = '07';
echo $fromUrl == $typed ? 'match' : 'no match', "\n";
// `!=` is the only inequality operator, and it is exactly the opposite of
// `==`. This prints `changed`.
string $saved = 'draft';
string $current = 'published';
echo $saved != $current ? 'changed' : 'unchanged', "\n";
Output
same
no match
changed

User settings over defaults

<?nvs
// Shows how to combine two arrays with `Core\Arr::underlay` and read one
// character with `Core\Str::at`. The program reads settings and an order code.
// What this user saved, and the defaults for everything else.
array<string> $saved = ['theme' => 'dark', 'pageSize' => '50'];
array<string> $defaults = ['theme' => 'light', 'pageSize' => '25', 'language' => 'en'];
// `Core\Arr::underlay` keeps the first array's value at each shared key and
// adds the keys only the second array has. This prints `dark 50 en`.
array<string> $settings = Core\Arr::underlay($saved, $defaults);
echo $settings['theme'], ' ', $settings['pageSize'], ' ', $settings['language'], "\n";
// `Core\Str::at` reads the character at one position, and `Core\Str::slice`
// reads several from a position. This prints `A AT`.
string $orderCode = 'AT-1042';
echo Core\Str::at($orderCode, 0), ' ', Core\Str::slice($orderCode, 0, 2), "\n";
Output
dark 50 en
A AT

Three equals signs do not compile

<?nvs
// Shows that `===` does not compile. The error says to write `==`.
// proof: compile-error
int $quantity = 1;
if ($quantity === 1) {
echo "one item\n";
}
Output
error[E0232]: `===` is not supported
--> 04-three-equals-signs-do-not-compile.nvs:6:15
|
6 | if ($quantity === 1) {
| ^^^ Novis keeps exactly one equality operator
= help: use `==` — it never converts either operand, so there is nothing for `===` to distinguish
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Details

Each form has its own error code:

  • and, or and xor are the error E0226.
  • === and !== are the error E0232, and <> is the error E0241.
  • A cast such as (int)$x or (string)$x is the error E0225.
  • @ before an expression is the error E0236. Catch the error with try and catch instead.
  • $a = &$b is the error E0701.
  • $$name and ${$name} are the error E0202.
  • $a + $b on two arrays is the error E0467.
  • $s[0] on a string is the error E0482.
  • $s++ on a string is the error E0474.
  • eval(…) is the error E0201. Put the code in a file and require it.

A backtick and &$x in an expression give a general syntax error. That error does not name what to write instead. To run another program, use Core\Process. To let a method change a variable, use an inout parameter.

After ===, !== or <>, the compiler reads the line as if == or != were written. So it also reports every other error in the file in the same run.

Functions and forms that do not exist

Some ways of writing code that PHP allows do not exist in Novis. Writing one is a compile error.

The list is eval, extract, settype, compact and the other free functions, variable variables such as $$name and ${$expr}, list(...), casts such as (int)$x, the @ sign, =&, backticks, die, include and require_once, yield used as a value, and self, static or parent written outside a class. Each error but the backtick's names what to write instead: a method under Core for a free function, as for a conversion, [...] for taking a list apart, require for reading another file, and exit for ending a program.

Good to know: you meet all of these when you compile, never while the program is running.

The examples below show the three replacements you need most often: a Core method in place of a free function, as in place of a cast, and [...] in place of list(...). The last example shows the error for a variable variable.

A core method in place of a php function

<?nvs
// Shows the `Core` methods that replace PHP's free functions. In Novis
// every built-in is a method on a class, so `strtoupper($s)` is written
// `Core\Str::upper($s)`.
use Core\Str;
array<string> $names = ['ada lovelace', 'grace hopper'];
foreach ($names as string $name) {
// `Str::upper` returns the text in capitals, and `Str::length` returns the
// number of characters. The first line prints `ADA LOVELACE (12)`.
echo Str::upper($name), ' (', Str::length($name), ")\n";
}
// `Str::replace` puts one text in place of another. This prints
// `grace-hopper`.
echo Str::replace('grace hopper', ' ', '-'), "\n";
Output
ADA LOVELACE (12)
GRACE HOPPER (12)
grace-hopper

As in place of a cast

<?nvs
// Shows `as`, the one way to convert a value in Novis. PHP writes `(int)$x`
// or `settype($x, "int")`; neither exists here, and both are replaced by this
// one syntax.
string $typed = '42';
// `as int` converts the text to a whole number, and the result goes into a new
// variable. This prints `44`.
int $age = $typed as int;
echo $age + 2, "\n";
// A conversion that cannot work throws an error. `as ?int` returns `null`
// instead, so you can test the result. This prints `not a number`.
?int $maybe = "twelve o'clock" as ?int;
echo $maybe ?? 'not a number', "\n";
// `as` never changes the variable it reads. `$typed` is still the text it was
// given, so this prints `42`.
echo $typed, "\n";
Output
44
not a number
42

Reading a row into variables

<?nvs
// Shows how to read the parts of a row into variables, the way a program does
// when it imports a file. PHP offers `list(...)`, `extract` and `$$name` for
// this; Novis has `[...]` for a list and an array you index by name.
array<string> $line = ['2026-09-19', 'ada@example.com', '19.99'];
// `[...]` on the left takes the elements in order, one variable each. This
// prints the three values on one line.
[string $date, string $email, string $amount] = $line;
echo $date, ' ', $email, ' ', $amount, "\n";
array<string> $form = ['email' => 'grace@example.com', 'country' => 'US'];
// A row whose parts have names is an array with string keys. You read the key
// you want, and `??` gives a value for a key that is not there.
echo $form['email'], "\n";
echo $form['phone'] ?? 'no phone given', "\n";
// The key can come from a variable. This replaces a variable variable such as
// `$$name`, and the result still has a type: here it is a `string`.
string $wanted = 'country';
echo $form[$wanted], "\n";
Output
2026-09-19 ada@example.com 19.99
grace@example.com
no phone given
US

A variable variable

<?nvs
// Shows that a variable variable such as `$$name` does not exist, so this
// program does not compile. Use an array with a key for each name instead.
// proof: compile-error
string $name = "value";
string $value = "x";
echo $$name;
Output
error[E0202]: variable variables (`$$name` / `${expr}`) are not supported
--> 04-a-variable-variable.nvs:8:6
|
8 | echo $$name;
| ^^^^^^ defeats name resolution and type inference
= help: use an explicit `array<T>` instead — its keys are `string`, so one entry per name
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Details

Each form below is a compile error with its own code. The help line of the error names what to write instead.

  • eval(…) is E0201. Put the code in a file and require it, or run it with spawn script.
  • extract($a) is E0205. Use destructuring, or read each key.
  • settype($x, "int") is E0208. Convert into a new variable with as.
  • compact(…), strlen(…) and every other free function is E0320. Use a Core method such as Core\Str::length($s).
  • $$name and ${$expr} are E0202. Use an array<T> with a key for each name.
  • list($a, $b) = $p; is E0230. Write [int $a, int $b] = $p;.
  • (int)$x and the other casts are E0225. Write $x as int.
  • @$a["k"] is E0236. Catch the error with try and catch.
  • $b =& $a; is E0701. Write $b = $a;, which copies the value.
  • die is E0228. Write exit.
  • include and require_once are E0221. Write require.
  • In a generator, a yield whose value is used, such as mixed $x = yield 1;, is E0448. Write yield 1; as a statement of its own.
  • self, static and parent outside a class are E0303. This is the same for self::X, static::$x, parent::f() and new self(). Write the class name.

A backtick is the one form with no replacement. ` echo ls; is the error E0001, an unexpected character, and its message names nothing else. To run another program, use Core\Process::run` with the path and an array of arguments.

  • Do add parentheses when you are not sure which operator runs first.
  • Do read the compiler warning about ?? and ? :. It shows both possible meanings.
  • Don’t write -2 ** 2 when you mean (-2) ** 2.
  • Don’t write long expressions with many operators. Store a part in a variable with a clear name.