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):

asp

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

asp

→ This is clean, safe, readable.

Way 2 – Bad habit (still very common in old code): No Dim

asp

→ 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

asp

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)

asp

4. Full Realistic Example – Mini Shopping Cart Summary

asp

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! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *