Creating and Using Variables

Description

Covers a number of topics relating to variables. Topics include: using operators and calculating values, Xbasic operators, using variables inside HTML tags, outputting variables, and more.

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

Case Is Irrelevant

Alpha Anywhere 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.

  • Make the names meaningful, so that you can understand what they are. Length is not a problem, but longer names invite spelling errors.

  • All names should begin with a letter (A..Z or a..z). You can also use numbers and underscore "_" characters. Do not use spaces or other symbols.

  • Many programmers also prefix a variable name with a tag that indicates the variable's type (character, numeric, logical, etc.). This helps to avoid programming errors. For example: Lastname becomes cLastname and Quantity becomes nQuantity , indicating that the first is a character variable and the second is a numeric variable.

  • Do not use any of the reserved words as variables. This will always cause program errors.

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:

The following types of operators are available.

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

    <head>
    <meta name="generator" content="Alpha Anywhere 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>
  2. Select File > Save As.

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

  4. Click 'X' to close the HTML Editor.

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

  6. 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.

    images/WPT_Creating_and_Using_Variables.gif
  7. 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 Anywhere HTML Editor">
    <title>Creating and Using Variables</title>
    </head>
    <body>
    <p>The mileage is 80
    
    </body></html>

    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 Anywhere 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>
    2. Select File > Save As.

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

    4. Click 'X' to close the HTML Editor.

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

    6. 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.

      images/WPT_Large_Text.gif

    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 Anywhere 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 Anywhere 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 + ".
        "
        ? "He was born on " + dBirthday + " at " + time("h:m AM",tWhen) + ".
        "
        ? "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 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 + ".
        "
      • 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) + ".
        "
      • 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.

    2. Select File > Save As.

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

    4. Click 'X' to close the HTML Editor.

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

    6. 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.

      images/WPT_Displaying_variables.gif