๐งฎ 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:
username
user1
user_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 Explicit
to 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
(orSet
for objects) - Use
Option Explicit
to 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 :