Chapter 30: ASP Variables
ASP Variables — the very next lesson after “ASP Syntax” in the famous W3Schools ASP Tutorial.
This lesson is extremely important because almost everything you do in Classic ASP starts with variables: storing user names, prices, dates, database results, counters, flags, form data, session info — everything!
I will explain it like your favorite teacher who actually wrote Classic ASP sites in 2003–2008 — slowly, clearly, with many real examples, showing both good and bad habits, and explaining exactly what happens on the server.
1. The Golden Rule of Variables in Classic ASP
In Classic ASP (.asp files with VBScript):
- Variables are created with Dim
- You can use them withoutDim — but this is very dangerous
- Variables are always Variant type (they can hold string, number, date, object, Nothing — no strict typing)
- They are case-insensitive (Age = age = AGE)
- Scope is usually page-level (unless put in Session or Application)
Best practice (write this 10 times):
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> |
Option Explicit forces you to declare every variable with Dim. Without it, typos create new variables silently — one of the #1 sources of bugs in old ASP sites.
2. Three Ways to Declare Variables (with Examples)
Way 1 – Good way: Dim + Option Explicit
|
0 1 2 3 4 5 6 |
<% Dim name Dim age Dim price Dim isLoggedIn Dim today name = "Rahul Sharma" age = 27 price = 399.99 isLoggedIn = True today = Now() Response.Write "<p>Name: " & Server.HTMLEncode(name) & "</p>" Response.Write "<p>Age: " & age & "</p>" Response.Write "<p>Price: ₹" & FormatNumber(price, 2) & "</p>" Response.Write "<p>Logged in? " & isLoggedIn & "</p>" Response.Write "<p>Today: " & FormatDateTime(today, vbLongDate) & "</p>" %> |
→ This is clean, safe, readable.
Way 2 – Bad habit (still very common in old code): No Dim
|
0 1 2 3 4 5 6 |
<% name = "Rahul Sharma" ' ← creates variable automatically age = 27 price = 399.99 Response.Write name & " is " & age & " years old." %> |
→ Looks shorter, but very risky: If you mistype nage = 30 later → new variable created, no error, bug silently appears.
Way 3 – Multiple variables in one Dim
|
0 1 2 3 4 5 6 |
<% Dim name, age, price, isAdmin, lastLogin name = "Priya" age = 24 price = 199.50 isAdmin = False lastLogin = #2/16/2026# %> |
3. Where Variables Live – Scope in Classic ASP
| Scope | Declaration place | Lifetime / Visibility | Typical use |
|---|---|---|---|
| Page | Inside <% … %> block | Only on current page request | Local temp variables |
| Session | Session(“key”) = value | Until browser closes or Session.Abandon | User login, shopping cart |
| Application | Application(“key”) = value | Until IIS restarts or app pool recycles | Global counters, site settings |
| Global | In included .inc file (outside blocks) | Current page + included files | Rarely used now |
Real example – Session variable (very common)
|
0 1 2 3 4 5 6 |
<!-- login.asp --> <% Dim username username = Request.Form("username") If username <> "" Then Session("LoggedInUser") = username Response.Redirect "welcome.asp" End If %> <!-- welcome.asp --> <% If Session("LoggedInUser") = "" Then Response.Redirect "login.asp" Else Response.Write "<h1>Welcome back, " & Server.HTMLEncode(Session("LoggedInUser")) & "!</h1>" End If %> |
4. Full Realistic Example – Mini Shopping Cart Summary
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h2>Your Shopping Cart</h2> <% Dim items, prices, i, total total = 0 ' Simulate cart from Session (real code would read from DB or Session array) items = Array("Hyderabadi Biryani", "Mutton Haleem", "Irani Chai", "Osmania Biscuit") prices = Array(399.00, 450.00, 80.00, 45.00) If UBound(items) >= 0 Then %> <table border="1"> <tr><th>Item</th><th>Price</th></tr> <% For i = 0 To UBound(items) Response.Write "<tr>" Response.Write "<td>" & Server.HTMLEncode(items(i)) & "</td>" Response.Write "<td>₹" & FormatNumber(prices(i), 2) & "</td>" Response.Write "</tr>" total = total + prices(i) Next %> </table> <p><strong>Grand Total: ₹<%= FormatNumber(total, 2) %></strong></p> <% Else Response.Write "<p>Your cart is empty.</p>" End If %> </body> </html> |
5. Teacher Summary – Classic ASP Variables in One Page
ASP Variables (Classic ASP with VBScript) means:
- Declare with Dim name (strongly recommended with Option Explicit)
- No strict types — everything is Variant (string, number, date, object…)
- Case-insensitive (Age = age)
- Scope: page (default), Session(“key”), Application(“key”)
- Output with Response.Write or short <%= … %>
- Always use Server.HTMLEncode() on user-controlled data
- No modern features (no List<T>, no decimal literal m, no string interpolation)
This is how millions of small websites worked 1998–2010, and many old Indian business/government systems still run this exact style in 2026.
Questions for next class?
- Want to see Session + login full example?
- Or database recordset into variables?
- Or compare Classic ASP variables vs Razor C# variables side-by-side?
- Or shall we continue with the W3Schools ASP track (next is usually ASP Procedures)?
Just tell me — I’m here! 🚀🇮🇳 Happy learning, Webliance! 😊
