Chapter 2 The DynaScript Language


Variables

You do not have to declare DynaScript variables or assign them to a fixed datatype. A variable must have a value before you use it in an expression; otherwise a runtime warning is generated.

Variable names

Each variable is identified by a case-sensitive name. Variable names must start with a lowercase or uppercase letter or an underscore character ( _ ). Subsequent characters can be letters, underscores, or digits. No other characters are allowed in variable names.

These are examples of valid variable names:

x
_magic
current_product
Product99

These are invalid variable names:

x 1
3magic
current!product
Product-99
R&D

Declaring variables

Although you are not required to declare variables in DynaScript, Sybase recommends that you do so. This is not only good programming style, but can also avoid scoping problems with other global variables. Declare a variable using the var keyword. This statement declares a variable named x and assigns it a value of 35 :

var x = 35;

For more information on the var statement, see "Statements".

Datatypes

Scripts use the following datatypes:

Table 2-1: Datatypes

Datatype

Example

Undefined

any variable that has not been assigned a value.

Numeric

1.23456 or 3

Boolean(logical)

true or false

Strings

"this is a string"

Null

denotes a null value

Function

writeln

Object

site

For more information on the operators that work with these datatypes, see "Operators".

Assigning values to variables

Assign a value to a variable using the = operator, just as in many other programming languages. In DynaScripts, however, assigning a value to a variable also sets the datatype of the variable.

For example, the following statement assigns the value 23 to the variable x :

x = 23 ;

If no variable named x exists in the current scope, one is automatically created.

You can later assign a different datatype to the variable x , using an assignment statement like this:

x = "Now I am a string" ;

The following two statements successively assign a Boolean value of true and then a null (unknown) value to the variable status :

status = true ;
status = null ;

Variable scope

Variables have a scope: that is, they exist for part or all of a script or a template. You can use global variables anywhere in a template, but you can use local variables only within the current function.

If you do not explicitly declare a variable, it is created as a global variable. For example, if the first time that the variable x is referred to is in a statement like this:

x = 35 ;

then x is a global variable, and can be used (and will have the same value) anywhere in the HTML template.

If you declare a local variable with the same name as an existing global variable, references to that variable name will use the local variable.

 


Copyright © 1999 Sybase, Inc. All rights reserved.