ASP Lesson #3 - Learn What A Variable Is And How To Use It

Submitted on: 1/5/2015 10:59:00 AM
By: Daniel Larsson (from psc cd)  
Level: Beginner
User Rating: By 10 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 1884
     In this lesson the actual ASP learning starts. My past two lesson was written so you can run ASP on your computer, and also a brief introduction to what ASP is and when to use it.

Here I will introduce you to the most basics of ASP and ALL script languages.....Variables. If you don't know how to use this, you won't get far in your Coding.

It might be intresting to read for you guys that aren't newbies, since I also explain the differens between Constants and Variables, and also very short about Arrays and Functions.

If you've read it, please rate it or comment so I know what im doin right/wrong when writing these guides.

ASP Lesson #3 - Learn What Variables Are

At last! Now we will be looking into some real ASP Code. In this third lesson I will teach you how Variables work and what a Constant is. I will also introduce you to Dynamic Variables and a little function called Array. I will also teach you how to show the content of the variables in the webbrowser.


Let us first understand what a variable is since variables are things you will use in almost every script you will write in the future. A variable is, very simple, a name of a value. This value don't have to be determined directly, instead you can determine this after the user/visitor has done something at your site that you asked him to do. Similar, you can determine that the value is being calcuated by a formula that you choose.

The reason to why we don't determine the variables at once, is that we don't know which value the variable may get from your visitor. Similar, it's easier to work with a varible if you name it to something you can remeber, not just x,y,z,n etc. More about this later in this lesson.

A variable can't just be only one value, it can also be a generic name for something that shall be executed.In an exempel, a formula that must be run at more than one point in the script. Then it's unnecessary and clumsy that you re-write this formula at all points you wish to add your formula. Instead, use a variable and name it to Formula1 or whatever you wish to name it. To do this, just tell you script that this Variable is Equal to this formula.

Think about all the examples in this lesson might be redicilous and that you might not find any useful ways to use this scripts! But variables are something VERY basic for ASP, and even if we might be doing some redicilous ( sorry if i spell this word wrong, tell me how it should be spelled and I fix it ) things rigth now, you must understand this clearly before we proceed. Else it will only be hard for you in the future lessons.

Ok hope I have explained well for you what a variable is, and why we almost use it in every script. In the next part I will teach you how to write the code when you use a variable.


A script, almost, allways contains more variables, there are only few that uses one. So the first thing you must do when you write ASP Code is that you must declare which variables the script shall contain and use.Example is below:


With these lined i have Defined four variabled. Tal, Mer, Siffra and Presentera. These for variables have named that I have figured out which means that they don't exist in own names in the VBScript Language. But the first word, Dim, is the word you must use everytime you would like to define a variable.

When you are about to define more than one variable you use a comma(,) between every variable to separate them from each other. Remeber that if you don't to this, two variables can be misstaken as one, or there will be an error in the script.

Now you know how to define variables in VBScript, but now I'll teach you how to allocate values to the variables.


What I just did was: Give the two variables Tal and Mer different values. Everwhere you use the variables Mer and Tal in your script it will be treated as 10 and 20. At the end of this lesson I will teach and show you how to use the variables together.


Const means Constant. This works in the sam way as a Variable with one exception. The value that we allocate the const will allways be constant. In other words, you can't vary this value.

Once you have allocated a value to a const, it will allways be that value until you change it in the line/row that you defined the const. A const is defined at the same way as a Variable, with one exception. You bothe declare and define a const at the same row.

Const Word="I like apples"

As you can see, the only difference is that a const is defined and declared at the same row. If you use Dim to define variables, you must declare them at a separate row.


I will only show you some very basics about what array is since I will be writing much about arrays later in this guide. As you know Variables usually contains some sort of a value, but maybe you like it to contain many values at the same time?

Dim Cars

Now the variable named Cars have four different values. As you can see you place the different values within Paranteeses (), quotemarks and separates them with a comma (,). Note: All variables that are allocated values that isn't clean numbers (1,2,3 etc) must be written inside quotemarks ("variable") and be separated by a comma.

The reason why we use a parenteese () around these values depends, in this case, that we use the function Array ( I will talk about functions in another lesson ).


You use ReDim whenever you want to allocate a dynamic generic variable.

Dim Cars()

This indicates that the variable Cars still doesn't have any values, but from this moment on exists and is ready to accept values. If you would like to allocate the variable Cars more values you use ReDim.

ReDim Cars(300)

Present Variables' Values

Okey, now you know what Dim, Const, Array and ReDim is. Now I will teach you how a simple script with a few variables will be written. I will also teach you how to present them in your webbrowser. Example:

Dim Number,More,Big,Total



The total amount of these numbers is: <b><%=Total%></b>.


Okey. What happened here? Since this is a complete script that will be rendered in the webbrowser we must first start with "Blockers" or Delimiters that tells the webbrowser that this is some ASP Code. Then we will write our script. In this example, the script defined and declared four variables and printed the total amount of these variables on the webpage. We also gave the four variables different values.

The last variable, Total is the amount of the variables Number,More and Big. When the variables have digital values (0-9) you can simply add them together using a plussign (+) between them.

Now the script is ready and we finish the script by using a reverse block (%>). The computer now knows what the answer will be, but the code will not show anything in the webbrowser. To do this, we must ad some simple html code. First we write a sentance, The total amount of these numbers is, if we don't write anything here a useless number will be displayed.

Since we wan't to show the variable Total we mark it a little extra with a B tag (<b>). To finish, I write the variable Total within to Blockers, note: I also add a = sign before I write total. This means that the answer will be printed here.

You show a variables value by adding a = sign.


Okey. You can now create a file in your ASP folder and run the script in your webbrowser. If you've done it right it should show the total amount of the three numbers we declared and defined =). If you have done something wrong, then check your code again. If it shows the correct amount in the page then view the source and look. Just look =). All the ASP code will be gone, and only simple HTML code will be there. Don't remember that the file must be named as *.asp!

End Of Lesson #3

Other 5 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.