Free Webmaster Help Logo
Tutorials Articles Tools Images Directory Ask Newsletter

Advanced HTML
Frames & Tables
Rounded Table Corners
Flash 5
Site Promotion
Mobile Internet: WML/WAP
Server Side Includes (SSI)
HTML - The Basics


Related Links
More ASP Sites

Sitemap | Contact | Link To Us | Advertise
Report A Problem
Home : Tutorials : ASP : Part 2

ASP Tutorial
Part 2 - Output And Variables


In the last part I explained a little about how to write ASP and how to tell the server that you have ASP code in your file and what language it is written in. In this part I will explain what is probably the most important use of ASP: output.

Sending Output To The Browser

It's always been a tradition of programming tutorials to begin by writing the simple 'Hello World' program, so this one won't make an exception! Sending output is done using the ASP command:


so to write 'Hello World' to the user's browser the complete code would be:

<%@ Language=VBScript %>
Response.Write("Hello World")

Again, this code begins by telling the system that you are writing in VBScript. Then comes the Response.Write command. Basically this is made up of two parts. 'Response' tells the server that you want to send information to the user. There are other types of command including: Request (which gets information from the user), Session (for user session details), Server (for controlling the server) and Application (for commands relating to the application). More about these later.

The second part, 'Write', tells the server that the type of response you would like to send is to write information to the user's browser. This doesn't just have to be text, but can include variables, which will be discussed in more depth later in this tutorial.


Probably the most important feature of a programming language is a variable. A variable is basically a way of storing text, numbers or other data, so that it can be referenced later. For example, to change the earlier 'Hello World' script:

<%@ Language=VBScript %>
OutputText = "Hello World"

The output of this code will be exactly the same as the first script, but it is fundementally different as it uses variables. Basically what this code does follows:

OutputText = "Hello World"

This line sets up a variable called OutputText and stores in it the string of letters 'Hello World'. As this is now stored in a variable, you can now reference this text you have stored in any part of your script, and you can also manipulate it. The next line:


tells the server that you are sending information to the browser, and that the information to be sent is the contents of the variable called OutputText. Please note that the variable name is not enclosed in quotation marks. If you did this the browser would simply output the title of the variable as text.

There is a second way of outputting the values of variables, other than using Response.Write. The earlier code could have been written:

<%@ Language=VBScript %>
OutputText = "Hello World"

In this example, the = sign is used instead of ResponseWrite.

Variable Operations

The main benefits to storing information in variables is that you can use the text over and over again. For example, once storing "Hello World" in the variable OutputText, I can then use it in various places in my code:

<%@ Language=VBScript %>
OutputText = "Hello World"

This is my <% =OutputText %> script. The whole reason for it is to output the text <% =OutputText %> to the browser.

which would display in the browser:

This is my Hello World script. The whole reason for it is to output the text Hello World to the browser.

You can also do various operations on text stored in variables using len, left and right.

The len function simply tells you how many characters are in a string, so if you used the following code:

<% =len(OutputText) %>

The server would return to the browser the
length of the text stored in OutputText, in this case "Hello World", so the browser would display the number 11 on the screen. You could also assign this value to a variable using:

<% StringLength = len(OutputText) %>

which would set the value of the variable called StringLength to 11.

You can also use the functions left and right. These will display only part of the variable. For example:

<% =left(OutputText, 2) %>

which would display:


and the code:

<% =right(OutputText, 4) %>

would display:


Basically, these functions take the number of characters specififed from the left or right of the string, so left("Some Text", 5) takes the first 5 characters of the text.

Part 3

In part three I will show you how to make conditional statements using IF, THEN and ELSE.

© 1999 - 2001