Operators#
Like algebra from school where x=4
, y=6-2
, and z=x+y
; the characters =
, -
, and +
are operators.
Operator Types#
MivaScript provides the following types of operators:
- Arithmetic operators
- Comparison operators
- Logical operators
- Text string operators
- Bitwise operators
Operator Precedence#
MivaScript operators have a built-in precedence that determines which operator gets processed first when two or more operators are expressed. You can override the built-in precedence by surrounding sub-expressions with parentheses. From highest to lowest:
++
,--
NOT
ROUND
,CRYPT
,MOD
,SUBSTR
,POW
/
,*
+
,-
,$
IN
,CIN
,EIN
,ECIN
,EQ
,NE
,GE
,LE
,LT
,GT
,ISNULL
AND
,OR
Increment (++
) and decrement (--
) are supported inside expressions (both pre- and post- forms).
<MvEVAL EXPR="{ l.var++ }">
<MvASSIGN NAME="l.array" INDEX="{ ++l.pos }" VALUE="{ l.var }">
<!--
Pre-increment/decrement always happens before other operations:
- `l.row * ++l.col` (increment before multiplication)
- `--l.col + l.row` (decrement before addition)
Operator precedence:
- `l.row * l.col++` (increment after multiplication)
- `l.col++ + l.row` (increment after addition)
-->
Incorrect
"{ 10 + 8 * 0.3366 ROUND 2 }"
Evaluated as:
10 + 8 = 18
18 * 0.3333 = 5.9994
5.9994 ROUND 2 = 5.99
(wrong result)
Incorrect
"{ 0.3366 ROUND 2 * 8 + 10 }"
Evaluated as:
0.3366 ROUND 2 = 0.34
0.34 * 8 = 2.72
2.72 + 10 = 12.72
Correct
Use parenthesis to explicitly declare how the formula should be evaluated. Parenthesis are evaluated first.
{ 10 + ((8 * 0.3366) ROUND 2) }
Which yields:
(8 * 0.3366) = 2.6928
(2.6928 ROUND 2) = 2.69
10 + 2.69 = 12.69
(Correct Result)
Arithmetic Operators#
Used with numbers or numeric expressions. A minus before a variable returns null
; to express a negative value use { -1 * x }
or { 0 - x }
.
Operator | Name | Description / Example |
---|---|---|
+ |
Addition | { 10 + 2 } = 12 |
++ |
Increment | { variable++ } or { ++variable } |
- |
Subtraction | { 12 - 2 } = 10 |
-- |
Decrement | { variable-- } or { --variable } |
* |
Multiplication | { 10 * 2 } = 20 |
/ |
Division | { 20 / 2 } = 10 |
POW |
Power | 3 POW 4 = 81 |
MOD |
Modulo | 21 MOD 5 = 1 |
ROUND |
Rounding | { 1.494 ROUND 2 } = 1.49 |
Comparison Operators#
Compare two numeric or string expressions; return 1
(true
) or 0
(false
). String comparisons are case-sensitive.
Operator | Name | Description / Example |
---|---|---|
EQ |
Equal to | { 12 EQ 12 } = 1 |
GT |
Greater than | { 12 GT 2 } = 1 |
LT |
Less than | { 2 LT 12 } = 1 |
NE |
Not equal | { 12 NE 2 } = 1 |
GE |
Greater or equal | { 12 GE 12 } = 1 |
LE |
Less or equal | { 2 LE 12 } = 1 |
<MvASSIGN NAME="l.posn" VALUE="{ 1 }">
<MvWHILE EXPR="{ l.posn LE 10 }">
<MvIF EXPR="{ l.posn MOD 2 }">
l.posn is Odd<br>
<MvELSE>
l.posn is Even<br>
</MvIF>
<MvASSIGN NAME="l.posn" VALUE="{ l.posn + 1 }">
</MvWHILE>
Loops while l.posn ≤ 10
, alternating odd/even.
Logical Operators#
Determine logical relationships between values. Use parentheses for clarity.
Operator | Name | Description / Example |
---|---|---|
NOT |
Logical NOT | { NOT (0 LT 2) } = 0 |
AND |
Logical AND | { (1 LT 2) AND (3 GT 2) } = 1 |
OR |
Logical OR | { (1 LT 2) OR (1 GT 2) } = 1 |
ISNULL |
Tests NULL | { ISNULL '' } = 1 |
<MvIF EXPR="{ ISNULL g.Email }">
Please enter an email address.<br>
<MvELSEIF EXPR="{ g.Subject AND g.Message AND g.PhoneNumber }">
...Sending your email...
<MvELSE>
Please enter a Subject, Message, and PhoneNumber<br>
</MvIF>
All three fields must be non-empty for the AND
to return true
.
Text String Operators#
Used with strings or string expressions.
Operator | Name | Description / Example |
---|---|---|
$ |
Concatenate strings | { 'Scooby ' $ 'Doo' } = 'Scooby Doo' |
IN / CIN |
Beginning position | { 'oo' IN 'Scooby Doo' } = 3 / case-insensitive CIN |
EIN / ECIN |
End position | { 'y D' EIN 'Scooby Doo' } = 8 / ECIN |
CRYPT |
Encrypt a string | { user_password CRYPT(substring(s.dyn_time_t,8,2)) } |
<MvASSIGN NAME="g.name" VALUE="{ 'Scooby' $ ' ' $ 'Doo' }">
<MvASSIGN NAME="g.first_name" VALUE="{ substring(g.name,1,(' ' IN g.name)-1) }">
<MvASSIGN NAME="g.last_name" VALUE="{ substring(g.name,(' ' IN g.name)+1,len(g.name)) }">
Mr. <MvEVAL EXPR="{ g.last_name $ ', ' $ g.first_name }"><br>
Result: Mr. Doo, Scooby
Bitwise Operators#
Operate on numbers at the binary (“bit”) level.
Operator | Name | Description / Example |
---|---|---|
BITAND |
Bitwise AND | expr1 BITAND expr2 |
BITOR |
Bitwise OR | expr1 BITOR expr2 |
BITXOR |
Bitwise XOR | expr1 BITXOR expr2 |
BITOC |
Ones complement | BITOC expr flips bits (e.g. BITOC 9 = -10) |
BITSL |
Shift-left | expr_a BITSL expr_b (e.g. {23 BITSL 2} = 92) |
BITSR |
Shift-right | expr_a BITSR expr_b |
Converts a character to its hexadecimal representation.
<MvASSIGN NAME="l.character" VALUE="z">
<MvASSIGN NAME="l.byte" VALUE="{ asciivalue(l.character) }">
<MvASSIGN NAME="l.msb" VALUE="{ (l.byte BITAND 240) BITSR 4 }">
<MvIF EXPR="{ l.msb GT 9 }">
<MvASSIGN NAME="l.msb" VALUE="{ asciichar(97 + (l.msb - 10)) }">
</MvIF>
<MvASSIGN NAME="l.lsb" VALUE="{ l.byte BITAND 15 }">
<MvIF EXPR="{ l.lsb GT 9 }">
<MvASSIGN NAME="l.lsb" VALUE="{ asciichar(97 + (l.lsb - 10)) }">
</MvIF>
<MvEVAL EXPR="{ l.character }"> = HEX <MvEVAL EXPR="{ l.msb $ l.lsb }">