Creating and Using Variables

Using the DIM Statement

When creating an A5W page you use the DIM statement to create new variables.

<%a5

dim cFirstname as C = "Fred"

dim nAge as N = 12

dim dBirthday as D = {08/03/1992}

dim lMarried as L = .F.

dim tWhen as T = ctodt("08/03/1992 3:23:34 pm")

%>

When you use the DIM statement, you may define the following types of variables

Type

Data Example

Code Example

Character

"Washington"

dim cLastname as C

Date

{07/04/1776}

dim dBirthday as D

Date Time

"08/03/1992 3:23:34 pm"

dim tWhen as T

Logical

.T.

dim lMarried as L

Number

1984

dim nAge as N

Short Time

"3:23:34 pm"

dim tTimeOfDay as Y

Case Is Irrelevant

Alpha Five and the Xbasic programming language do not care whether variable names are upper or lower case, or even a mixture of the two. The follow three statements are identical in functionality.

<%a5

dim clastname as C

dim cLASTNAME as C

dim cLASTname as C

%>

Naming Variables

There are several guidelines for naming variables.

Using Operators and Calculating Values

After you create some variables in an A5W page, what can you do with them? There are a large number of operators and functions that process one or two variables and produce a result. For example:

  1. Create a new A5W page with the following content.

<html>

<head>

<meta name="generator" content="Alpha Five HTML Editor">

<title>Creating and Using Variables</title>

</head>

<body>

<p>

<%a5

dim nSpeed as N

dim nHours as N

dim nMiles as N

nSpeed = 40

nHours = 2

nMiles = nSpeed * nHours

? "The milenAge is " + nMiles

%>

</p>

</body></html>

  1. Select File > Save As.

  2. Enter "Creating and Using Variables" in the File name field and click Save.

  3. Click to close the HTML Editor.

  4. Display the A5W Pages page of the Web Projects Control Panel.

  5. Right click the "Creating and Using Variables" entry and select Publish (Local Webroot) and open. Your new page will appear in your Internet browser. The results should look like this.  [Picture]

Note how the page displays the value of nMiles. If you look at the source of the page from your Internet browser, you will see.

<html>

<head>

<meta name="generator" content="Alpha Five HTML Editor">

<title>Creating and Using Variables</title>

</head>

<body>

<p>The mileage is 80</p>

</body></html>

Xbasic Operators and Functions

The following types of operators are available.

Data Type

Operator

Description

Numeric

See Also : Mathematical Functions

+

Addition

-

Subtraction

*

Multiplication

/

Division

^

Exponent

**

Exponent

MOD()

Modular Arithmetic

MIN()

The minimum of 2 values

MAX()

The maximum of 2 values

Logical

.AND.

Combine two values and return either .T. (TRUE) or .F. (FALSE).

.NOT.

.OR.

.XOR.

Character

See Also : Character Functions

+

Concatentation Operator

-

Concatentation with Trim

$

Substring Inclusion Operator

!$

Substring Exclusion Operator

Date & Time

See Also : Date and Time Functions

+

Date Addition

Date Time Addition

Time Addition

-

Date Subtraction

Date Time Subtraction

Time Subtraction

Using Variables Inside HTML Tags

As you saw above, you can use variables inside of an A5W page to store and then display a value. You can also use a variable inside an HTML tag when you need to assign a value to a parameter.

  1. Create a new A5W page with the following content.

<html>

<head>

<meta name="generator" content="Alpha Five HTML Editor">

<title>Large Text</title>

</head>

<body>

<%a5

dim nSize as N

nSize = 5

%>

<p>

<font size = <%a5 ? nSize %> >Large Text</font>

</p>

</body>

</html>

  1. Select File > Save As.

  2. Enter "Large Text" in the File name field and click Save.

  3. Click to close the HTML Editor.

  4. Display the A5W Pages page of the Web Projects Control Panel.

  5. Right click the "Large Text" entry and select Publish (Local Webroot) and open. Your new page will appear in your Internet browser. The results should look like this.  [Picture]

Note how the page displays the "Large Text". If you look at the source of the of the page from your Internet browser, you will see.

<html>

<head>

<meta name="generator" content="Alpha Five HTML Editor">

<title>Large Text</title>

</head>

<body>

<p>

<font size = 5.000000>Large Text</font>

</p>

</body>

</html>

Outputting Variables

The following page displays different types of variables. Note how the DIM statements also assign values to the variables as they are created.

  1. Create a new A5W page with the following content.

<html>

<head>

<meta name="generator" content="Alpha Five HTML Editor">

<title>Displaying Variables</title>

</head>

<body>

<p> <%a5

dim cFirstname as C = "Fred"

dim nAge as N = 12

dim dBirthday as D = {08/03/1992}

dim lMarried as L = .F.

dim tWhen as T = ctodt("08/03/1992 3:23:34 pm")

? "His name is " + cFirstname + ".<br>"

? "He was born on " + dBirthday + " at " + time("h:m AM",tWhen) + ".<br>"

? "Since he is " + nAge + " years old, it is not surprising that he "

if (lMarried = .F.) then

? "is not"

else

? "is"

end if

? " married."

%> </p>

</body>

</html>

The character variable cFirstname has an initial value of "Fred". Note how the ordinary HTML text and the <br> tag are combined with the cFirstname variable using the plus "+" character and output with a single question mark "?" at the beginning of the line.

dim cFirstname as C = "Fred"

...

? "His name is " + cFirstname + ".<br>"

The date variable dBirthday has an initial value of {08/03/1992} and the tWhen variable has an initial value of 3:23:34 pm (after reformatting by the CTODT() function). Note how the single output statement prints both the dBirthday and tWhen variables. Since the HTML page wants a character value to display, we use the TIME() function to convert the time value to a formatted character string.

dim dBirthday as D = {08/03/1992}

dim tWhen as T = ctodt("08/03/1992 3:23:34 pm")

? "He was born on " + dBirthday + " at " + time("h:m AM", tWhen) + ".<br>"

Next, we print out the nAge variable. No special formatting is required.

dim nAge as N = 12

? "Since he is " + nAge + " years old, it is not surprising that he "

Finally, we test the value of the lMarried variable, and make a decision about which of two character strings to output.

dim lMarried as L = .F.

if (lMarried = .F.) then

? "is not"

else

? "is"

end if

? " married."

To see the Displaying Variables page.

  1. Select File > Save As.

  2. Enter "Displaying Variables" in the File name field and click Save.

  3. Click to close the HTML Editor.

  4. Display the A5W Pages page of the Web Projects Control Panel.

  5. Right click the "Displaying Variables" entry and select Publish (Local Webroot) and open. Your new page will appear in your Internet browser. The results should look like this.  [Picture]

See Also

Contents

Supported By

Alpha Five Version 6 and Above