ASP Script Variables
By Stephen Bucaro
In a computer program a variable is analogous to a mailbox or envelope where the
program can store data and that storage location might be referenced by other
parts of the program.
In ASP you don't have to declare the "type" of a variable. All variables
are of the Variant type, which can contain an integer, string, or other type.
To declare a variable use the Dim statement. If you want to print out the value
of a variable, you can use the ASP Response.Write command.
Shown below example code creating and assigning values to variables:
Dim txtDays, numDays
txtDays = "The number of days in a week is "
numDays = 7
Response.Write(txtDays & numDays)
In the code shown above, the ampersand is used to concatenate the two values.
The output would be: The number of days in a week is 7.
In ASP it's possible to use a variable without declaring it. This is called
default declaration. However, this would be bad programming practice and
can leads to errors and should be avoided. To create ASP code that is more
free from errors, place the Option Explicit statement at the top of the code.
Option Explicit On
Option Explicit demands explicit declaration of all variables in the code.
If the code contains Option Explicit statement, than an attempt to use an
undeclared variable causes an error.
There are some rules you must follow when naming variables in ASP:
• A variable name must begin with a letter, not a number or underscore
• It cannot have more than 255 characters
• It cannot contain a period, a space or dash
• They cannot be a keyword (some times called reserved word)
• A variable's name must be unique in the same scope
Variables declared using the Dim Keyword at the script level are available to
all the procedures within the same script. If you replace Dim by "Private",
the variable will be available only within that script in which they are declared.
If you replace Dim by "Public", the variable will be available to all the
procedures across all the associated scripts.
A variable will be kept in memory only for the life of the current page.
when the current page has finished executing the variable and will be released
from memory. To declare a variable available to more than one ASP file, declare
it as an application variable or a session variable.