Special Operators
Ternary ? :
The ternary operator is a shortcut for 'if a, then b, else c'.
It is used as an expression to determine a value.
$a = condition ? $result1 : $result2 // Same as... if condition { $a = $result1 } else { $a = $result2 } // Examples $a = true ? 'Yes' : 'No' //= 'Yes' $b = false ? 'Yes' : 'No' //= 'No' $badge = $isVerified ? '(Verified User)' : ''
Modulo %
Modulo gives you the remainder after dividing two numbers.
If the right side of the operator is negative, the result will also be negative.
10 % 2 //= 0 10 % 3 //= 1 10 % 4 //= 2 -10 % 3 //= 1 10 % -3 //= -1 // negative right side -10 % -3 //= -1 // negative right side
Odd/Even
Modulo is often used to check if a number is odd or even.
However, for clarity, you can simply call methods on the number itself.
$num = 20 $num.isEven() //= true $num.isOdd() //= false $num.isMultipleOf(5) //= true $num.isMultipleOf(7) //= false
Orset & Andset
Logic and assignment can be combined into a single operator.
Orset ||=
Orset (OR Set) can be used to assign a default value.
$numGuests ||= 1 // same as... if !$numGuests: $numGuests = 1
Andset &&=
Andset (AND Set) can be used to process a value further, if it exists.
$date &&= formatDate($date) // same as... if $date: $date = formatDate($date)
Orval & Andval
These operators return the value of either side, depending on whether the left value is true or false.
Orval ||:
Orval (OR Value) can be used to assign a default value.
$id = $user.id ||: -1 // same as... if $user.id { $id = $user.id } else { $id = -1 }
Andval &&:
Andval (AND Value) can be used to check if an object is valid before accessing it further.
$id = $user &&: $user.id // same as... if $user { $id = $user.id } else { $id = $user // falsey value }
Splat ...
The splat ...
prefix operator can be used to pack or unpack function arguments to/from Lists.
Packing
Use splat in a function signature to combine a variable number of arguments into a single List.
fn dropFirst($first, ...$rest) { return $rest } dropFirst(1, 2, 3, 4) //= [2, 3, 4]
Unacking
Use splat in a function call to expand a List into individual arguments.
fn add($n1, $n2, $n3) { return $n1 + $n2 + $n3 } $nums = [11, 22, 33] add(...$nums) //= 66
Cat ^^
The cat ^^
prefix operator applies advanced opti-meow-zation to a variable.
$moneyInBank = 301.67 print(^^$moneyInBank) //= 'MEOW'
Spaceship <=>
The spaceship operator compares two numbers and returns -1, 0, or 1, depending if the 1st number is less than, equal, or greater than the 2nd number.
This is primarily used for sorting functions.
1 <=> 2 //= -1 1 <=> 1 //= 0 2 <=> 1 //= 1 $letters = q[x y z] $letters.sort(x{ $b <=> $a }) //= ['z', 'y', 'x']
Bitwise Operators
Because bitwise operations are rarely used in web development, THT uses a non-convential syntax (borrowed from Raku) to avoid accidental confusion with their Logical counterparts (e.g. &
vs. &&
).
+| bitwise OR +& bitwise AND +^ bitwise XOR +~ bitwise NOT +> bitwise SHIFT RIGHT +< bitwise SHIFT LEFT
These can be used with literal binary digits with the 0b
prefix.
Example:
$bin = 0b100 +& 0b110 //= 4 (0b100)
Match
In place of switch
, there will be a match
operator, similar to what was introduced in PHP 8.
Coming soon.