Classic ASP โ Variables โ Declare and Use Data in VBScript
Introduction โ What Are Variables in Classic ASP?
In Classic ASP, variables are used to store and manipulate data during the server-side execution of a script. Whether you’re saving user input, storing counters, or performing calculations, variables are essential to control logic and output in .asp files.
Classic ASP uses VBScript by default, so variable handling follows VBScript’s dynamic typing and syntax rules.
In this guide, youโll learn:
- How to declare and assign variables in Classic ASP
- Data types and implicit conversions
- Real examples of variable usage in output and logic
- Best practices for clean and safe code
Declaring Variables โ Dim
All Classic ASP variables are declared using Dim.
<%
Dim name
name = "Vaibhav"
%>
VBScript is case-insensitive and loosely typedโyou don’t need to declare the type of a variable.
Assigning Values
<%
Dim age
age = 25
%>
You can assign:
- Strings (
"text") - Numbers (
123) - Dates (
#06/11/2025#) - Objects (
Set myObj = Server.CreateObject(...))
Example โ Displaying Variable Output
<%
Dim product
product = "Laptop"
Response.Write "Product: " & product
%>
Output:
Product: Laptop
Performing Calculations
<%
Dim x, y, total
x = 10
y = 5
total = x + y
Response.Write "Total: " & total
%>
Output:Total: 15
String Concatenation
Use & to concatenate strings:
<%
Dim firstName, lastName
firstName = "John"
lastName = "Doe"
Response.Write "Hello " & firstName & " " & lastName
%>
Output: Hello John Doe
Working with Arrays
<%
Dim colors
colors = Array("Red", "Green", "Blue")
Response.Write colors(0) 'Output: Red
%>
Variable Scope
| Scope | Where Defined | Lifetime |
|---|---|---|
| Local | Inside a function | Until function exits |
| Page-Level | In the page | Until page finishes |
| Session | Session("key") | Until session ends |
| Application | Application("k") | Until app restarts or ends |
Using Option Explicit for Safer Code
Place at top of the page to enforce explicit declaration:
<%@ Language="VBScript" %>
<%
Option Explicit
Dim username
username = "admin"
%>
Without it, typos in variable names go unnoticed:
usrname = "admin" ' Incorrect but no error without Option Explicit
Variable Naming Rules
Valid:
usernameuser1user_Name
Invalid:
1name(starts with a number)user-name(hyphens not allowed)Dim(VBScript keyword)
Best Practices for ASP Variables
Do:
- Always declare variables with
Dim - Use
Option Explicitto catch typos - Use meaningful names (
email,priceTotal, etc.)
Avoid:
- Relying on undeclared or global variables
- Reusing variable names in loops/functions
- Using reserved keywords as variable names
Summary โ Recap & Next Steps
Variables in Classic ASP allow you to store and process data dynamically. Using Dim, you can declare string, numeric, or array values, combine them in expressions, and display results with Response.Write.
Key Takeaways:
- Declare variables using
Dim(orSetfor objects) - Use
Option Explicitto avoid bugs - Combine variables with HTML using
&and<%= %>
Real-world Use Cases:
- Capture user input from forms
- Display user-specific messages
- Process calculations in shopping carts or reports
FAQs โ Classic ASP Variables
Do I need to declare the type of a variable?
No. Classic ASP uses VBScript, which is dynamically typed.
What does Option Explicit do?
Forces you to declare variables before use. Helps prevent bugs from typos.
How do I store a variable across pages?
Use Session("variable") = value to persist data across requests.
Share Now :
