
Asp Variables in ASP
Sure! Here's a clear overview of Variables in Classic ASP (using VBScript):
ASP Variables
In Classic ASP, variables are used to store data values. VBScript (the default ASP scripting language) is loosely typed, meaning you don’t declare the variable type explicitly.
Declaring Variables
Use the Dim
statement to declare variables:
<%Dim name, age, pricename = "Alice"age = 30price = 19.99%>
Variable Types
VBScript variables are variants — they can hold any data type:
String:
"Hello"
Integer / Double:
100
,3.14
Boolean:
True
orFalse
Date:
#2025-05-30#
Object: (e.g., database connection objects)
Array: collections of values
Assigning Values
<%Dim xx = 10x = x + 5x = "Now a string" ' Can change type anytime%>
Using Variables
You can output variable values like this:
<%Dim usernameusername = "Bob"Response.Write "Welcome, " & username%>
Or shorthand:
Welcome, <%= username %>
Constants
Constants hold fixed values and are declared with Const
:
<%Const Pi = 3.14159Response.Write "Pi = " & Pi%>
Scope of Variables
Variables declared with
Dim
inside<% %>
have page-level scope.To share variables across pages for the same user, use
Session
.To share variables globally for all users, use
Application
.Variables inside functions or subroutines are local by default.
Example: Using Variables
<%Dim firstName, lastNamefirstName = "Jane"lastName = "Doe"Response.Write "Hello, " & firstName & " " & lastName%>
If you want help with arrays, objects, or best practices for variable naming and usage in ASP, just ask!