Variables#
In programming languages, variables are symbolic “containers” for storing information. Like algebra from school where x=4
, y=6
, and z=x+y
; x
and y
are variable names, 4
and 6
are values, and z=x+y
is an expression.
MivaScript variables are used to hold values which can be strings, numbers, lists of data, or even other variables. Variables can be populated by a simple assignment (like x=3
or tree="oak"
) or by the result of an expression (like name = 'Miva ' + 'Script'
).
Variable Names#
A variable can have a short name, like g.x
, or a more descriptive name, like l.product:code
, but all variable names follow these rules:
- Names start with one of these prefixes and a period (
g.
,l.
,s.
, ord.
). Periods can only be used after the prefix. - Names are case-insensitive (
g.name
andg.NAME
are the same variable). - After the prefix, names must begin with a letter or the underscore character.
- Variable names consist of the letters a–z (upper and lower case), the digits
0–9
, or the underscore (_
). - Bracket characters are used to designate array indexes (
[ ]
). - The colon (
:
) is used to indicate a member of a structure.
Other characters are permitted if they are escaped with a backslash, but this is not common practice nor recommended.
<MvEVAL EXPR="{age\-old}">
This evaluates to the variable age-old
; without the backslash, this expression would be interpreted as the value of age
minus the value of old
.
Variable Scope#
Prefixes in the variable name define its scope in the current script or function. If scope is not defined, the virtual machine checks to see if a previously scoped variable of the same name exists and has scope in the current location, in this order:
- System (
s.
orsystem.
): Built-in system variables (read-only). - Local (
l.
orlocal.
): Only inside a user-defined<MvFUNCTION>
block. - Database (
.d.
or.database.
): Separator between a database alias and field name (e.g.,products.d.code
). - Global (
g.
): Scope is the entire program. - Dynamic (
dyn_
): Dynamically generated time values (see Time Variables).
Valid Names#
Correct
<MvEVAL EXPR="{ _username }">
<MvEVAL EXPR="{ user_name }">
<MvEVAL EXPR="{ g._user_name }">
<MvEVAL EXPR="{ l.user_name }">
<MvEVAL EXPR="{ l.users }">
<MvEVAL EXPR="{ l.users[1]:firstname }">
<MvEVAL EXPR="{ l.users[l.index]:lastname }">
Invalid Names#
Incorrect
<MvEVAL EXPR="{1st_username}">
<MvEVAL EXPR="{user.name}">
<MvEVAL EXPR="{:username}">
Variable Typing#
MivaScript does not require that variables be declared or initialized before use, thus they are type-less. The same variable can contain numbers, text, dates, or boolean (true/false) values. In MivaScript a zero or null value is considered false; all other values are considered true.
<MvASSIGN NAME="g.birthday" VALUE=338961600> Unix style date number.
<MvASSIGN NAME="g.birthday" VALUE="09/28/1980"> String
<MvASSIGN NAME="g.birthday" VALUE="September 28, 1980"> String
<MvASSIGN NAME="g.birthday" VALUE="{ s.dyn_time_t EQ 338961600 }"> This expression is false;
<MvIF EXPR="{ g.birthday }">
Evaluates as true for the first three g.birthdays and false for the last one.
</MvIF>
It is up to you to enforce data typing in your programs. Type-less variables can be the source of program bugs, so care must be exercised. Both examples generate compiler errors because of mixing string and numeric data in a single expression. However, the final example works, producing a string even if all variables are numbers, although “9 28, 1980” is probably not the result you want.
Information
Operator type does not match constant type; inefficiency or unexpected rounding may occur.
<MvASSIGN NAME="g.birthday" VALUE="{ 'September, ' $ 28 $ ', ' $ 1980 }">
Information
Operator type does not match constant type; inefficiency or unexpected rounding may occur.
<MvASSIGN NAME="g.birthday" VALUE="{ 'September, ' + 28 + ', ' + 1980 }">
Information
This works because everything is a string.
<MvASSIGN NAME="g.birthday" VALUE="{ 'September, ' $ '28, ' $ '1980' }">
Information
This works even if ALL the variables are numbers, but the result “9 28, 1980” is probably not what you wanted.
<MvASSIGN NAME="g.birthday" VALUE="{ g.month $ ' ' $ g.day $ ', ' $ g.year }">
One possible strategy for avoiding these kinds of problems is to declare string types in the variable name itself, e.g. g.str_birthday
.
HTML Variable Names#
When an HTML form is submitted, the name
and value
of form fields are received by MivaScript as global variables. In this example, Sales_Reports.mvc
receives four global variables: g.screen
, g.action
, g.username
, and g.password
, and can take action based on their values.
Form Field Variables:#
<form method="get" action="Sales_Reports.mvc">
<input type="hidden" name="screen" value="report" />
<input type="hidden" name="action" value="login" />
<input type="text" name="username" value="" size="25" />
<input type="password" name="password" value="" size="25" />
<input type="submit" value="Search">
</form>