Option Explicit in Classic ASP

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.

Syntax and where to put Option Explicit

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.

Declaring and using variables with Dim

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

Why Option Explicit is useful: catching misspelled names

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.

Without Option Explicit: a silent mistake

<%@ 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.

With Option Explicit: the name is rejected

<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v  ' Intentional error
%>
Expected error: VBScript reports an undefined variable (v) instead of silently continuing. This snippet is intentionally incorrect to illustrate the difference; use the corrected example below for normal execution.

Correct the name and run the page

<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim v1
v1 = 2
Response.Write "Value of v1=" & v1
%>

Output:

Value of v1=2

Declare variables, arrays and procedure variables

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.

StatementPurposeExample
DimDeclare a variable or arrayDim userName
PublicDeclare a public script-level variablePublic pageTitle
PrivateDeclare a private script-level variablePrivate counter
ReDimSize or resize a dynamic arrayReDim countries(2)

A fixed array

Dim countries(2)
countries(0) = "India"
countries(1) = "Canada"
countries(2) = "Japan"
Response.Write countries(1)

Output:

Canada

A dynamic array with ReDim Preserve

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().

Page-level and local variables

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.

Practical example: reading a Classic ASP form

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.

Option Explicit in applications with included ASP files

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.

Common mistakes and troubleshooting

ProblemWhy it happensFix
Variable is undefinedA name was never declared, or it was misspelled.Add Dim for the intended name or fix the spelling.
Option Explicit placed too lateIt appears after script commands or HTML output.Move it immediately after ASP directives and before other page content.
Option Explicit On failsThis is not the Classic ASP VBScript syntax.Use Option Explicit alone.
Works until shared code is includedAn include uses undeclared variables or changes statement order.Review the include and test variable declarations and placement.
Wrong value despite declarationsThe name is declared but the assigned value or logic is wrong.Debug the input and conditions; Option Explicit checks names, not business logic.

FAQs

Is Option Explicit required in Classic ASP?

No. VBScript allows implicit variables if the statement is absent, but using it is recommended because it catches undeclared names and spelling mistakes.

What is the difference between Option Explicit and Dim?

Option Explicit enables the requirement to declare variable names. Dim declares a particular variable.

Does Option Explicit define a variable as Integer or String?

No. VBScript variables generally hold Variant values. The statement does not enforce a fixed data type or prevent incorrect values.

Can I add Option Explicit inside a Sub or Function?

No. Place it at the beginning of the script before other statements; declare procedure-local names inside the procedure with Dim.

Will Option Explicit fix every Classic ASP error?

No. It catches undeclared names, but it does not replace validation, output encoding, runtime checks or debugging.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer