Option Explicit tells VBScript to require a declaration for each variable before it is used. Add Option Explicit at the beginning of your Classic ASP script, followed by declarations such as Dim v1. This catches misspelled variable names before they cause confusing results.
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v1
%>
Output:
Value of v1=2
This is a complete short ASP example. Place it in an .asp file on a server running Classic ASP; the PHP tutorial file displays the code but does not execute it.
Option Explicit
In Classic ASP, the Option Explicit statement must come after any ASP page directives and before HTML text or other VBScript commands. The placement matters; do not add it halfway through a page.
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head><title>Option Explicit example</title></head>
<body>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v1
%>
</body>
</html>
Use Option Explicit (not Options Explicit). The syntax Option Explicit On belongs to a different Visual Basic language variant; use only Option Explicit in VBScript. See VBScript variables for the different declaration forms.
This keeps the original lesson: declare v1, assign the number 2, and display it. Dim declares the variable; assigning the value is a separate operation.
Dim v1 ' Declare variable v1
v1 = 2 ' Assign a value
Response.Write "Value of v1=" & v1
Output:
Value of v1=2
You can declare several variables in one Dim statement and then assign values to them.
Dim firstName, lastName, fullName
firstName = "Ada"
lastName = "Lovelace"
fullName = firstName & " " & lastName
Response.Write Server.HTMLEncode(fullName)
Output:
Ada Lovelace
The original tutorial demonstrates the mistake of writing v rather than v1. Without Option Explicit, VBScript can create an implicit variable named v; its empty value may produce incomplete output instead of alerting you to the typo.
<%@ Language="VBScript" %>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v ' Typo: v instead of v1
%>
Output:
Value of v1=
The output is incomplete because the undeclared v does not contain the value 2.
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v ' Intentional error
%>
v) instead of silently continuing. This snippet is intentionally incorrect to illustrate the difference; use the corrected example below for normal execution.<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v1
%>
Output:
Value of v1=2
With Option Explicit, all variable names must be declared. Common VBScript declarations include Dim, Public, Private and ReDim. This requirement does not make VBScript strongly typed; ordinary variables are still Variants.
| Statement | Purpose | Example |
|---|---|---|
Dim | Declare a variable or array | Dim userName |
Public | Declare a public script-level variable | Public pageTitle |
Private | Declare a private script-level variable | Private counter |
ReDim | Size or resize a dynamic array | ReDim countries(2) |
Dim countries(2)
countries(0) = "India"
countries(1) = "Canada"
countries(2) = "Japan"
Response.Write countries(1)
Output:
Canada
Dim countries()
ReDim countries(1)
countries(0) = "India"
countries(1) = "Canada"
ReDim Preserve countries(2)
countries(2) = "Japan"
Response.Write Join(countries, ", ")
Output:
India, Canada, Japan
For more on arrays and bounds, see declaring arrays and array size with UBound().
A variable declared at page level is available to code in that script scope. A variable declared inside a procedure is local to that procedure. Option Explicit applies to variable names in either location.
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim pageTitle
pageTitle = "Plus2net ASP"
Sub ShowMessage()
Dim message
message = "Welcome to " & pageTitle
Response.Write Server.HTMLEncode(message)
End Sub
Call ShowMessage()
%>
Output:
Welcome to Plus2net ASP
The local name message is declared inside the Sub, while pageTitle is declared outside it. Both are explicitly declared before use.
When processing submitted data, declare the variable that will hold each value. The form-field name itself is a quoted string, not a VBScript variable. Encode user-entered text before displaying it in HTML.
<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim visitorName
visitorName = ""
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
visitorName = Trim(Request.Form("visitor_name"))
End If
%>
<!DOCTYPE html>
<html>
<head><title>ASP name form</title></head>
<body>
<form method="post" action="">
<label for="visitor_name">Your name:</label>
<input type="text" name="visitor_name" id="visitor_name" required>
<button type="submit">Submit</button>
</form>
<%
If Len(visitorName) > 0 Then
Response.Write "<p>Hello, " & Server.HTMLEncode(visitorName) & "</p>"
End If
%>
</body>
</html>
The value is read using Request.Form("visitor_name"). A declaration error in a form-processing page is easier to diagnose than a silently misspelled variable. For additional examples, see Classic ASP forms and the Request object.
Classic ASP applications often use server-side includes for shared declarations and procedures. Keep Option Explicit at the beginning of the page, after any ASP directives and before other page content. Declare variables used by your VBScript code, including those referenced from included scripts.
<%@ Language="VBScript" %>
<% Option Explicit %>
<!--#include file="common-functions.asp" -->
<%
Dim pageTitle
pageTitle = "My ASP page"
Response.Write Server.HTMLEncode(pageTitle)
%>
An include file should not emit HTML before the page’s Option Explicit statement. When migrating an existing application, review shared includes for undeclared variables and test those pages on IIS. See server-side includes and file handling for related examples.
| Problem | Why it happens | Fix |
|---|---|---|
| Variable is undefined | A name was never declared, or it was misspelled. | Add Dim for the intended name or fix the spelling. |
| Option Explicit placed too late | It appears after script commands or HTML output. | Move it immediately after ASP directives and before other page content. |
Option Explicit On fails | This is not the Classic ASP VBScript syntax. | Use Option Explicit alone. |
| Works until shared code is included | An include uses undeclared variables or changes statement order. | Review the include and test variable declarations and placement. |
| Wrong value despite declarations | The name is declared but the assigned value or logic is wrong. | Debug the input and conditions; Option Explicit checks names, not business logic. |
No. VBScript allows implicit variables if the statement is absent, but using it is recommended because it catches undeclared names and spelling mistakes.
Option Explicit enables the requirement to declare variable names. Dim declares a particular variable.
No. VBScript variables generally hold Variant values. The statement does not enforce a fixed data type or prevent incorrect values.
No. Place it at the beginning of the script before other statements; declare procedure-local names inside the procedure with Dim.
No. It catches undeclared names, but it does not replace validation, output encoding, runtime checks or debugging.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.