Variables are a fundamental concept in many computer languages and knowing how to work with them is essential knowledge.
This tutorial focuses on:
- What is a variable?
- Declaring variables
- Naming variables
- Assigning values to variables
- Printing variables
What is a variable?
A variable is a container which stores information in a computer's memory. Variables store important data that is often times fundamental to a script. For example, you can use a variable to store a users name or the number of times a user visits a certain page on your website. The value of a variable can change all throughout a script.
In ASP a variable is declared with the Dim keyword. This is the same methodology as with VBScript since VBScript is the default language used in ASP.
You can declare variables separately or all at once.
When naming variables, several rules should be followed:
- Make sure that the variable name is descriptive - If you do not give a variable a descriptive name, it will be hard to understand what the variable refers to. For example, if you wanted to create a variable which would hold a value signifying the amount of chairs in a room, which variable name would be more appropriate - numChairs or a? The better choice for the variable name would be numChairs because it is more descriptive.
- Make sure the variable name is of appropriate length - Make sure the variable name is long enough to be descriptive, but not too long.
- Do not use spaces in variable names - Spaces are not allowed in variable names!
- Do not use periods (.) in variable names - Variable names cannot contain periods.
- Do not use special symbols in variable names such as !@#%^&* - As is the rules with spaces, special symbols are not allowed in variable names. Using special symbols in variable names will generate an error. There is however one special symbol that can be used in variable names, and that symbol is the underscore ( _ ) symbol. Variable names can only contain letters, numbers, or the underscore symbol.
- Do not exceed 255 characters - Variable names cannot exceed 255 characters.
Assigning values to variables
You can assign values to variables using the "=" sign.
You can print variables using the Response.Write() statement.