Article

ASP Language Basics

Page: 1 2 3 4 5 6 Next

Using Variables

Once created and assigned a value, a variable may be used anywhere the value it contains might be used. Let's look at a simple script that prints out the room temperature as stored in the variable intRoomTempC. Open your text editor and type the following (remember, don't type the line numbers), then save the file as PrintRoomTemp.asp:

1  <% Option Explicit %>  
2  <!-- PrintRoomTemp.asp  
3       A simple script that prints out a variable. -->  
4  <html>  
5  <head>  
6  <title> Room Temperature </title>  
7  </head>  
8  <body>  
9  <p>Room temperature in degrees Celsius:  
10 <%  
11 Dim intRoomTempC             ' Declare a variable  
12 intRoomTempC = 20            ' Assign it a value  
13 Response.Write intRoomTempC  ' Write out the value  
14 %>  
15 </p>  
16 </body>  
17 </html>

This program is a lot like the very simple script we saw above, with a few notable exceptions:

  • Line 1: We specify Option Explicit at the top of this file to guard against typing mistakes in variable names, as described above. When used, this should always appear at the top of the file, before even any HTML tags; otherwise, it will produce an error message.
  • Lines 2-3: This is just an HTML comment tag. It doesn't actually do anything, but I just thought I'd mention it in case you were not familiar with these.
  • Lines 11-12: As should be obvious to you by now, these two lines declare a variable called intRoomTempC and then assign it a value of 20.
  • Line 13: From the previous article, you should be used to seeing Response.Write used to print out some string of text (or see the example at the top of this article). In this case, however, the value following the command is not enclosed in quotes. As a result, instead of actually printing out "intRoomTempC", this line grabs the value stored in the intRoomTempC variable and prints that out.

Upload the file to your ASP-enabled Web host, or copy the file into the document root directory of the ASP-enabled Web server running on your computer, then open it in your browser by typing in the appropriate URL (e.g. http://www.myhost.com/~me/PrintRoomTemp.asp or http://localhost/PrintRoomTemp.asp). You should be presented with a Web page with the following text:

Room temperature in degrees Celsius: 20

Although the ASP code in this script effectively just prints out the number 20, the main purpose of this example was to demonstrate how to use a value stored in a variable. In this case, we printed the value out, but as you'll see in later examples, variables can be used in many other ways as well.

An alternate method exists for printing out a value stored in a variable is to use the ASP. By surrounding a variable or some other ASP expression with the special <%= ... %> delimiters, the value of that variable or expression will be placed in the page at that location. Have another look at the example we just saw, but this time I'm using this nice shortcut:

1  <% Option Explicit %>  
2  <!-- PrintRoomTemp.asp  
3       A simple script that prints out a variable. -->  
4  <html>  
5  <head>  
6  <title> Room Temperature </title>  
7  </head>  
8  <body>  
9  <%  
10 Dim intRoomTempC             ' Declare a variable  
11 intRoomTempC = 20            ' Assign it a value  
12 %>  
13 <p>Room temperature in degrees Celsius: <%=intRoomTempC%></p>  
14 </body>  
15 </html>

Notice how the code for creating the variable and assigning it a value must come before the special tag that prints out the variable's value. Although it may not look it, this special tag is still considered ASP code, and will be processed as such. So it makes sense that you should have to assign the variable its value before printing it out.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links